function mergePermissionsConfig(coreConfig, themeConfig, entityPermissions) {
  var _a;
  const result = [];
  const disabledIds = new Set((themeConfig == null ? void 0 : themeConfig.disabled) || []);
  for (const perm of coreConfig.systemPermissions) {
    if (disabledIds.has(perm.id)) {
      continue;
    }
    const override = (_a = themeConfig == null ? void 0 : themeConfig.overrides) == null ? void 0 : _a[perm.id];
    if (override) {
      result.push({
        ...perm,
        ...override,
        id: perm.id,
        // ID cannot be changed
        source: "core",
        disabled: false
      });
    } else {
      result.push({
        ...perm,
        source: "core",
        disabled: false
      });
    }
  }
  if (themeConfig == null ? void 0 : themeConfig.features) {
    for (const feature of themeConfig.features) {
      result.push({
        id: feature.action,
        // Map action to id
        label: feature.label,
        description: feature.description,
        category: feature.category || "Features",
        roles: feature.roles,
        dangerous: feature.dangerous,
        source: "theme",
        disabled: false
      });
    }
  }
  for (const entityPerm of entityPermissions) {
    result.push({
      ...entityPerm,
      source: "entity",
      disabled: false
    });
  }
  const uiSections = [
    ...coreConfig.uiSections,
    ...(themeConfig == null ? void 0 : themeConfig.uiSections) || []
  ];
  const entitiesSection = uiSections.find((s) => s.id === "entities");
  if (entitiesSection) {
    const entityCategories = new Set(entityPermissions.map((p) => p.category));
    entitiesSection.categories = Array.from(entityCategories);
  }
  return {
    permissions: result,
    uiSections,
    disabledIds
  };
}
function extractEntityPermissions(entitySlug, entityLabel, permissions) {
  const result = [];
  for (const action of permissions.actions) {
    result.push({
      id: `${entitySlug}.${action.action}`,
      label: action.label,
      description: action.description,
      category: entityLabel,
      roles: action.roles,
      dangerous: action.dangerous
    });
  }
  if (permissions.customActions) {
    for (const action of permissions.customActions) {
      result.push({
        id: `${entitySlug}.${action.action}`,
        label: action.label,
        description: action.description,
        category: entityLabel,
        roles: action.roles,
        dangerous: action.dangerous
      });
    }
  }
  return result;
}
export {
  extractEntityPermissions,
  mergePermissionsConfig
};
