import {
  ALL_PERMISSIONS,
  ALL_PERMISSIONS_SET,
  ALL_RESOLVED_PERMISSIONS,
  PERMISSIONS_BY_ROLE,
  ROLE_PERMISSIONS_ARRAY,
  PERMISSIONS_BY_CATEGORY,
  FULL_MATRIX,
  UI_SECTIONS,
  AVAILABLE_ROLES,
  TEAM_PERMISSIONS_BY_ROLE
} from "@nextsparkjs/registries/permissions-registry";
class PermissionService {
  /**
   * Check if a role has a specific permission - O(1)
   *
   * @param role - Team role to check
   * @param permission - Permission to verify
   * @returns True if role has permission
   *
   * @example
   * const canCreate = PermissionService.hasPermission('admin', 'customers.create')
   */
  static hasPermission(role, permission) {
    var _a;
    if (role === "owner") return ALL_PERMISSIONS_SET.has(permission);
    return ((_a = PERMISSIONS_BY_ROLE[role]) == null ? void 0 : _a.has(permission)) ?? false;
  }
  /**
   * Unified permission check - works for teams, entities, and features - O(1)
   *
   * This is the recommended method for checking any type of permission.
   * It checks across all permission sources:
   * - Team permissions (team.view, team.edit, team.members.invite, etc.)
   * - Entity permissions (customers.create, tasks.update, etc.)
   * - Feature permissions (page-builder.access, media.upload, etc.)
   *
   * @param role - Team role to check
   * @param action - Action to verify (e.g., 'team.edit', 'customers.create', 'page-builder.access')
   * @returns True if role can perform action
   *
   * @example
   * // Team permission
   * PermissionService.canDoAction('admin', 'team.edit')
   *
   * // Entity permission
   * PermissionService.canDoAction('admin', 'customers.create')
   *
   * // Feature permission
   * PermissionService.canDoAction('editor', 'page-builder.access')
   */
  static canDoAction(role, action) {
    var _a, _b;
    if (role === "owner") return true;
    if (action.startsWith("team.")) {
      return ((_a = TEAM_PERMISSIONS_BY_ROLE[role]) == null ? void 0 : _a.includes(action)) ?? false;
    }
    return ((_b = PERMISSIONS_BY_ROLE[role]) == null ? void 0 : _b.has(action)) ?? false;
  }
  /**
   * Get all permissions for a role - O(1)
   *
   * @param role - Team role
   * @returns Array of permissions for the role
   *
   * @example
   * const permissions = PermissionService.getRolePermissions('admin')
   */
  static getRolePermissions(role) {
    if (role === "owner") return ALL_PERMISSIONS;
    return ROLE_PERMISSIONS_ARRAY[role] ?? [];
  }
  /**
   * Get permissions by category - O(1)
   *
   * @param category - Permission category
   * @returns Array of resolved permissions in category
   *
   * @example
   * const customerPerms = PermissionService.getByCategory('customers')
   */
  static getByCategory(category) {
    return PERMISSIONS_BY_CATEGORY[category] ?? [];
  }
  /**
   * Get all permission categories
   *
   * @returns Array of category names
   *
   * @example
   * const categories = PermissionService.getCategories()
   */
  static getCategories() {
    return Object.keys(PERMISSIONS_BY_CATEGORY);
  }
  /**
   * Get full permission matrix for UI - O(1)
   *
   * Returns copies to prevent mutation of internal data.
   *
   * @returns Object with permissions, matrix, sections, and roles
   *
   * @example
   * const matrix = PermissionService.getMatrix()
   * // Use in Admin Panel permissions UI
   */
  static getMatrix() {
    return {
      permissions: [...ALL_RESOLVED_PERMISSIONS],
      matrix: { ...FULL_MATRIX },
      sections: [...UI_SECTIONS],
      roles: [...AVAILABLE_ROLES]
    };
  }
  /**
   * Check if a permission ID is valid - O(1)
   *
   * @param permission - Permission to validate
   * @returns True if permission exists
   *
   * @example
   * if (PermissionService.isValid('customers.create')) {
   *   // Valid permission
   * }
   */
  static isValid(permission) {
    return ALL_PERMISSIONS_SET.has(permission);
  }
  /**
   * Get permission configuration - O(n)
   *
   * @param permission - Permission to get config for
   * @returns Permission config or undefined
   *
   * @example
   * const config = PermissionService.getConfig('customers.delete')
   * if (config?.dangerous) {
   *   // Show warning
   * }
   */
  static getConfig(permission) {
    return ALL_RESOLVED_PERMISSIONS.find((p) => p.id === permission);
  }
  /**
   * Get all permissions
   *
   * @returns Array of all permission IDs
   *
   * @example
   * const allPerms = PermissionService.getAll()
   */
  static getAll() {
    return [...ALL_PERMISSIONS];
  }
  /**
   * Check if a role has ANY of the specified permissions
   *
   * @param role - Team role to check
   * @param permissions - Array of permissions to check
   * @returns True if role has at least one permission
   *
   * @example
   * const canManage = PermissionService.hasAnyPermission('member', [
   *   'customers.create',
   *   'customers.update'
   * ])
   */
  static hasAnyPermission(role, permissions) {
    return permissions.some((perm) => this.hasPermission(role, perm));
  }
  /**
   * Check if a role has ALL of the specified permissions
   *
   * @param role - Team role to check
   * @param permissions - Array of permissions to check
   * @returns True if role has all permissions
   *
   * @example
   * const canFullyManage = PermissionService.hasAllPermissions('admin', [
   *   'customers.create',
   *   'customers.update',
   *   'customers.delete'
   * ])
   */
  static hasAllPermissions(role, permissions) {
    return permissions.every((perm) => this.hasPermission(role, perm));
  }
}
const hasPermission = PermissionService.hasPermission;
const canDoAction = PermissionService.canDoAction;
const getRolePermissions = PermissionService.getRolePermissions;
const getPermissionsByCategory = PermissionService.getByCategory;
const getCategories = PermissionService.getCategories;
const getFullMatrix = PermissionService.getMatrix;
const isValidPermission = PermissionService.isValid;
const getPermissionConfig = PermissionService.getConfig;
const permissionRegistry = {
  hasPermission: PermissionService.hasPermission,
  canDoAction: PermissionService.canDoAction,
  getRolePermissions: PermissionService.getRolePermissions,
  getPermissionsByCategory: PermissionService.getByCategory,
  getFullMatrix: PermissionService.getMatrix,
  getPermissionConfig: PermissionService.getConfig,
  isValidPermission: PermissionService.isValid,
  isInitialized: () => true
};
export {
  PermissionService,
  canDoAction,
  getCategories,
  getFullMatrix,
  getPermissionConfig,
  getPermissionsByCategory,
  getRolePermissions,
  hasPermission,
  isValidPermission,
  permissionRegistry
};
