{"version":3,"file":"index.mjs","sources":["../../../../../src/lib/auth/active-directory/utils/index.ts"],"sourcesContent":["/**\n * Active Directory Group Mapping - Utilities\n *\n * Factory functions and utility helpers for group mapping.\n *\n * @module auth/active-directory/utils\n */\n\nimport type { ADGroupRoleMapping, ADGroupMappingConfig } from '../types';\nimport type { Role, Permission } from '../../types';\nimport { DEFAULT_GROUP_MAPPING_CONFIG } from '../ad-config';\nimport { ADGroupMapper } from '../core/mapper';\nimport { GROUP_MAPPING_PRESETS } from '../presets';\n\n// =============================================================================\n// Factory Function\n// =============================================================================\n\n/**\n * Create a group mapper with a preset configuration.\n *\n * @param preset - Preset name or custom mappings\n * @param options - Additional configuration options\n * @returns Configured ADGroupMapper\n */\nexport function createGroupMapper(\n  preset: keyof typeof GROUP_MAPPING_PRESETS | ADGroupRoleMapping[],\n  options?: {\n    appPrefix?: string;\n    defaultRole?: Role;\n    defaultPermissions?: Permission[];\n    cacheDuration?: number;\n  }\n): ADGroupMapper {\n  let mappings: ADGroupRoleMapping[];\n\n  if (Array.isArray(preset)) {\n    mappings = preset;\n  } else {\n    const presetFn = GROUP_MAPPING_PRESETS[preset];\n    if (typeof presetFn === 'function') {\n      mappings = (presetFn as (prefix: string) => ADGroupRoleMapping[])(\n        options?.appPrefix ?? 'app'\n      );\n    } else {\n      mappings = presetFn as ADGroupRoleMapping[];\n    }\n  }\n\n  return new ADGroupMapper({\n    mappings,\n    defaultRole: options?.defaultRole ?? ('guest' as Role),\n    defaultPermissions: options?.defaultPermissions ?? [],\n    cacheDuration: options?.cacheDuration,\n  });\n}\n\n// =============================================================================\n// Utility Functions\n// =============================================================================\n\n/**\n * Merge multiple group mapping configurations.\n *\n * @param configs - Configurations to merge\n * @returns Merged configuration\n */\nexport function mergeGroupMappings(\n  ...configs: Partial<ADGroupMappingConfig>[]\n): ADGroupMappingConfig {\n  const merged: ADGroupMappingConfig = {\n    ...DEFAULT_GROUP_MAPPING_CONFIG,\n    mappings: [],\n    defaultPermissions: [],\n  };\n\n  for (const config of configs) {\n    if (config.mappings) {\n      merged.mappings.push(...config.mappings);\n    }\n    if (config.defaultPermissions) {\n      merged.defaultPermissions.push(...config.defaultPermissions);\n    }\n    if (config.defaultRole) {\n      merged.defaultRole = config.defaultRole;\n    }\n    if (config.resolveNestedGroups !== undefined) {\n      merged.resolveNestedGroups = config.resolveNestedGroups;\n    }\n    if (config.cacheDuration !== undefined) {\n      merged.cacheDuration = config.cacheDuration;\n    }\n    if (config.fallbackBehavior) {\n      merged.fallbackBehavior = config.fallbackBehavior;\n    }\n  }\n\n  // Deduplicate permissions\n  merged.defaultPermissions = [...new Set(merged.defaultPermissions)];\n\n  return merged;\n}\n\n/**\n * Validate a group mapping configuration.\n *\n * @param config - Configuration to validate\n * @returns Validation errors if any\n */\nexport function validateGroupMapping(config: ADGroupMappingConfig): string[] {\n  const errors: string[] = [];\n\n  if (!Array.isArray(config.mappings)) {\n    errors.push('mappings must be an array');\n    return errors;\n  }\n\n  const identifiers = new Set<string>();\n\n  for (const [index, mapping] of config.mappings.entries()) {\n    if (!mapping.groupIdentifier) {\n      errors.push(`Mapping at index ${index}: groupIdentifier is required`);\n    }\n\n    if (!mapping.matchType) {\n      errors.push(`Mapping at index ${index}: matchType is required`);\n    } else if (!['exact', 'pattern', 'prefix', 'suffix'].includes(mapping.matchType)) {\n      errors.push(`Mapping at index ${index}: invalid matchType '${mapping.matchType}'`);\n    }\n\n    if (!mapping.role) {\n      errors.push(`Mapping at index ${index}: role is required`);\n    }\n\n    if (identifiers.has(mapping.groupIdentifier)) {\n      errors.push(\n        `Mapping at index ${index}: duplicate groupIdentifier '${mapping.groupIdentifier}'`\n      );\n    }\n    identifiers.add(mapping.groupIdentifier);\n  }\n\n  if (!config.defaultRole) {\n    errors.push('defaultRole is required');\n  }\n\n  return errors;\n}\n"],"names":["createGroupMapper","preset","options","mappings","presetFn","GROUP_MAPPING_PRESETS","ADGroupMapper","mergeGroupMappings","configs","merged","DEFAULT_GROUP_MAPPING_CONFI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