/** 当前登录管理员是否拥有 `required` 中任一权限（与 Laravel `prd.*` slug 对齐）。 */
export function adminHasAnyPermission(
  permissionSlugs: readonly string[] | null | undefined,
  required: readonly string[],
): boolean {
  const set = permissionSlugs ?? [];
  if (set.length === 0 || required.length === 0) {
    return false;
  }
  return required.some((slug) => set.includes(slug));
}

/** 当前登录管理员是否拥有 `required` 中任一 action code（与 API 鉴权 / `operational_permissions` 对齐）。 */
export function adminHasAnyPermissionCode(
  permissionCodes: readonly string[] | null | undefined,
  required: readonly string[],
): boolean {
  const set = permissionCodes ?? [];
  if (set.length === 0 || required.length === 0) {
    return false;
  }
  return required.some((code) => set.includes(code));
}

/** 读取会话中的 operational_permissions；旧缓存无该字段时回退到 permissions（兼容发版前 localStorage）。 */
export function adminOperationalPermissionCodes(
  profile: { operational_permissions?: string[]; permissions?: string[] } | null | undefined,
): readonly string[] {
  if (Array.isArray(profile?.operational_permissions) && profile.operational_permissions.length > 0) {
    return profile.operational_permissions;
  }
  return profile?.permissions ?? [];
}
