// src/utils/userGetInfoBz.ts
import { useUserStore } from '@/store/modules/user';

// 权限校验方法（替代指令的核心逻辑）
export const checkPermi = (perms: string[]): boolean => {
  const { permissions } = useUserStore();

  if (!Array.isArray(perms) || perms.length === 0) {
    throw new Error("权限参数格式错误，示例: checkPermi(['sys:user:edit'])");
  }

  return permissions.some((p) => p === '*:*:*' || perms.includes(p));
};

// 角色校验方法
export const checkRoles = (roles: string[]): boolean => {
  const { roles: userRoles } = useUserStore();

  if (!Array.isArray(roles) || roles.length === 0) {
    throw new Error("角色参数格式错误，示例: checkRoles(['admin'])");
  }

  return userRoles.some((r) => r === 'superadmin' || r === 'admin' || roles.includes(r));
};

// 操作列权限渲染器
export const actionRenderer: Handsontable.renderers.Base = (instance, td, row, col, prop, value, cellProperties) => {
  const rowData = instance.getDataAtRow(row);
  const canEdit = checkPermi(['amz:logisticsOrderDetail:edit']);
  const canDelete = checkPermi(['amz:logisticsOrderDetail:remove']);

  td.innerHTML = `
    <div class="htCenter">
      ${
        canEdit
          ? `
        <button class="btn-edit" data-row="${row}">
          <i class="el-icon-edit"></i>
        </button>
      `
          : ''
      }
      ${
        canDelete
          ? `
        <button class="btn-delete" data-row="${row}">
          <i class="el-icon-delete"></i>
        </button>
      `
          : ''
      }
    </div>
  `;

  return td;
};
