import { getSecuritySchemes } from "@scalar/api-client/v2/blocks/operation-block";
import { getSelectedSecurity } from "@scalar/api-client/v2/features/operation";
const getKey = (requirement) => Object.keys(requirement).sort().join(",");
const filterSelectedSecurity = (document, operation, selectedSecurityDocument, selectedSecurityOperation, securitySchemes = {}) => {
  const securityRequirements = operation?.security ?? document.security ?? [];
  const selectedSecurity = getSelectedSecurity(
    selectedSecurityDocument,
    selectedSecurityOperation,
    securityRequirements
  );
  const requirementSet = new Set(securityRequirements.map((r) => getKey(r)));
  const selectedRequirement = selectedSecurity.selectedSchemes[selectedSecurity.selectedIndex];
  if (selectedRequirement && requirementSet.has(getKey(selectedRequirement))) {
    return getSecuritySchemes(securitySchemes, [selectedRequirement]);
  }
  for (const selected of selectedSecurity.selectedSchemes) {
    if (requirementSet.has(getKey(selected))) {
      return getSecuritySchemes(securitySchemes, [selected]);
    }
  }
  if (operation?.security?.length) {
    return getSecuritySchemes(securitySchemes, securityRequirements.slice(0, 1));
  }
  return [];
};
export {
  filterSelectedSecurity
};
