const DEVELOPER_ROLE = "developer";
const MAX_HIERARCHY = 100;
const MAX_NON_DEVELOPER_HIERARCHY = 99;
const DEFAULT_HIERARCHY = 1;
const FALLBACK_DEFAULT_ROLE = "member";
const OWNER_TEAM_ROLE = "owner";
const OWNER_TEAM_HIERARCHY = 100;
const MAX_NON_OWNER_TEAM_HIERARCHY = 99;
const DEFAULT_TEAM_HIERARCHY = 1;
const FALLBACK_DEFAULT_TEAM_ROLE = "member";
function mergeRolesConfig(coreConfig, themeConfig) {
  const result = {
    coreRoles: coreConfig.coreRoles,
    availableRoles: [...coreConfig.coreRoles],
    defaultRole: coreConfig.defaultRole,
    hierarchy: { ...coreConfig.hierarchy },
    displayNames: { ...coreConfig.displayNames },
    descriptions: { ...coreConfig.descriptions }
  };
  if (!themeConfig) {
    return result;
  }
  if (themeConfig.additionalRoles && themeConfig.additionalRoles.length > 0) {
    const coreRolesSet = new Set(coreConfig.coreRoles);
    themeConfig.additionalRoles.forEach((role) => {
      if (coreRolesSet.has(role)) {
        console.warn(
          `[roles-merge] Theme attempted to add core role "${role}" via additionalRoles. Core roles cannot be redefined. Ignoring.`
        );
        return;
      }
      result.availableRoles = [...result.availableRoles, role];
    });
  }
  if (themeConfig.hierarchy) {
    Object.entries(themeConfig.hierarchy).forEach(([role, hierarchyValue]) => {
      if (role === DEVELOPER_ROLE && hierarchyValue !== MAX_HIERARCHY) {
        console.warn(
          `[roles-merge] Theme attempted to set developer hierarchy to ${hierarchyValue}. Developer role must always have hierarchy ${MAX_HIERARCHY}. Forcing to ${MAX_HIERARCHY}.`
        );
        result.hierarchy[role] = MAX_HIERARCHY;
        return;
      }
      if (role !== DEVELOPER_ROLE && hierarchyValue >= MAX_HIERARCHY) {
        console.warn(
          `[roles-merge] Theme attempted to set "${role}" hierarchy to ${hierarchyValue}. Non-developer roles cannot have hierarchy >= ${MAX_HIERARCHY}. Capping to ${MAX_NON_DEVELOPER_HIERARCHY}.`
        );
        result.hierarchy[role] = MAX_NON_DEVELOPER_HIERARCHY;
        return;
      }
      result.hierarchy[role] = hierarchyValue;
    });
  }
  result.availableRoles.forEach((role) => {
    if (!(role in result.hierarchy)) {
      console.warn(
        `[roles-merge] Role "${role}" is missing hierarchy value. Defaulting to ${DEFAULT_HIERARCHY}.`
      );
      result.hierarchy[role] = DEFAULT_HIERARCHY;
    }
  });
  if (themeConfig.displayNames) {
    result.displayNames = {
      ...result.displayNames,
      ...themeConfig.displayNames
    };
  }
  if (themeConfig.descriptions) {
    result.descriptions = {
      ...result.descriptions,
      ...themeConfig.descriptions
    };
  }
  if (themeConfig.defaultRole) {
    const availableRolesSet = new Set(result.availableRoles);
    if (!availableRolesSet.has(themeConfig.defaultRole)) {
      console.warn(
        `[roles-merge] Theme defaultRole "${themeConfig.defaultRole}" does not exist in available roles. Falling back to "${FALLBACK_DEFAULT_ROLE}".`
      );
      result.defaultRole = FALLBACK_DEFAULT_ROLE;
    } else {
      result.defaultRole = themeConfig.defaultRole;
    }
  }
  return result;
}
function mergeTeamRolesConfig(coreConfig, themeConfig) {
  const result = {
    coreTeamRoles: coreConfig.coreTeamRoles,
    availableTeamRoles: [...coreConfig.availableTeamRoles],
    defaultTeamRole: coreConfig.defaultTeamRole,
    hierarchy: { ...coreConfig.hierarchy },
    displayNames: { ...coreConfig.displayNames },
    descriptions: { ...coreConfig.descriptions }
  };
  if (!themeConfig) {
    return result;
  }
  const coreTeamRolesSet = new Set(coreConfig.coreTeamRoles);
  if (themeConfig.removeTeamRoles && themeConfig.removeTeamRoles.length > 0) {
    themeConfig.removeTeamRoles.forEach((role) => {
      if (coreTeamRolesSet.has(role)) {
        console.warn(
          `[roles-merge] Theme attempted to remove core team role "${role}". Core team roles cannot be removed. Ignoring.`
        );
        return;
      }
      result.availableTeamRoles = result.availableTeamRoles.filter((r) => r !== role);
      delete result.hierarchy[role];
      delete result.displayNames[role];
      delete result.descriptions[role];
    });
  }
  if (themeConfig.additionalTeamRoles && themeConfig.additionalTeamRoles.length > 0) {
    themeConfig.additionalTeamRoles.forEach((role) => {
      if (result.availableTeamRoles.includes(role)) {
        console.warn(
          `[roles-merge] Theme attempted to add existing team role "${role}". Ignoring.`
        );
        return;
      }
      result.availableTeamRoles = [...result.availableTeamRoles, role];
    });
  }
  if (themeConfig.hierarchy) {
    Object.entries(themeConfig.hierarchy).forEach(([role, hierarchyValue]) => {
      if (role === OWNER_TEAM_ROLE) {
        if (hierarchyValue !== OWNER_TEAM_HIERARCHY) {
          console.warn(
            `[roles-merge] Theme attempted to set owner team hierarchy to ${hierarchyValue}. Owner role must always have hierarchy ${OWNER_TEAM_HIERARCHY}. Ignoring.`
          );
        }
        return;
      }
      if (hierarchyValue >= OWNER_TEAM_HIERARCHY) {
        console.warn(
          `[roles-merge] Theme attempted to set "${role}" team hierarchy to ${hierarchyValue}. Non-owner roles cannot have hierarchy >= ${OWNER_TEAM_HIERARCHY}. Capping to ${MAX_NON_OWNER_TEAM_HIERARCHY}.`
        );
        result.hierarchy[role] = MAX_NON_OWNER_TEAM_HIERARCHY;
        return;
      }
      result.hierarchy[role] = hierarchyValue;
    });
  }
  result.availableTeamRoles.forEach((role) => {
    if (!(role in result.hierarchy)) {
      console.warn(
        `[roles-merge] Team role "${role}" is missing hierarchy value. Defaulting to ${DEFAULT_TEAM_HIERARCHY}.`
      );
      result.hierarchy[role] = DEFAULT_TEAM_HIERARCHY;
    }
  });
  if (themeConfig.displayNames) {
    result.displayNames = {
      ...result.displayNames,
      ...themeConfig.displayNames
    };
  }
  if (themeConfig.descriptions) {
    result.descriptions = {
      ...result.descriptions,
      ...themeConfig.descriptions
    };
  }
  if (themeConfig.defaultTeamRole) {
    const availableRolesSet = new Set(result.availableTeamRoles);
    if (!availableRolesSet.has(themeConfig.defaultTeamRole)) {
      console.warn(
        `[roles-merge] Theme defaultTeamRole "${themeConfig.defaultTeamRole}" does not exist in available team roles. Falling back to "${FALLBACK_DEFAULT_TEAM_ROLE}".`
      );
      result.defaultTeamRole = FALLBACK_DEFAULT_TEAM_ROLE;
    } else {
      result.defaultTeamRole = themeConfig.defaultTeamRole;
    }
  }
  return result;
}
export {
  mergeRolesConfig,
  mergeTeamRolesConfig
};
