{"version":3,"file":"index.mjs","sources":["../src/ui/index.ts"],"sourcesContent":["import type {\n  UiNode,\n  UiNodeAnchorAttributes,\n  UiNodeAttributes,\n  UiNodeGroupEnum,\n  UiNodeImageAttributes,\n  UiNodeInputAttributes,\n  UiNodeInputAttributesTypeEnum,\n  UiNodeScriptAttributes,\n  UiNodeTextAttributes,\n} from \"@ory/client\"\n\n/**\n * Returns the node's label.\n *\n * @param node\n * @return label\n */\nexport const getNodeLabel = (node: UiNode): string => {\n  const attributes = node.attributes\n  if (isUiNodeAnchorAttributes(attributes)) {\n    return attributes.title.text\n  }\n\n  if (isUiNodeImageAttributes(attributes)) {\n    return node.meta.label?.text || \"\"\n  }\n\n  if (isUiNodeInputAttributes(attributes)) {\n    if (attributes.label?.text) {\n      return attributes.label.text\n    }\n  }\n\n  return node.meta.label?.text || \"\"\n}\n\n/**\n * A TypeScript type guard for nodes of the type <a>\n *\n * @param attrs\n */\nexport function isUiNodeAnchorAttributes(\n  attrs: UiNodeAttributes,\n): attrs is UiNodeAnchorAttributes & { node_type: \"a\" } {\n  return attrs.node_type === \"a\"\n}\n\n/**\n * A TypeScript type guard for nodes of the type <img>\n *\n * @param attrs\n */\nexport function isUiNodeImageAttributes(\n  attrs: UiNodeAttributes,\n): attrs is UiNodeImageAttributes & { node_type: \"img\" } {\n  return attrs.node_type === \"img\"\n}\n\n/**\n * A TypeScript type guard for nodes of the type <input>\n *\n * @param attrs\n */\nexport function isUiNodeInputAttributes(\n  attrs: UiNodeAttributes,\n): attrs is UiNodeInputAttributes & { node_type: \"input\" } {\n  return attrs.node_type === \"input\"\n}\n\n/**\n * A TypeScript type guard for nodes of the type <span>{text}</span>\n *\n * @param attrs\n */\nexport function isUiNodeTextAttributes(\n  attrs: UiNodeAttributes,\n): attrs is UiNodeTextAttributes & { node_type: \"text\" } {\n  return attrs.node_type === \"text\"\n}\n\n/**\n * A TypeScript type guard for nodes of the type <script>\n *\n * @param attrs\n */\nexport function isUiNodeScriptAttributes(\n  attrs: UiNodeAttributes,\n): attrs is UiNodeScriptAttributes & { node_type: \"script\" } {\n  return attrs.node_type === \"script\"\n}\n\n/**\n * Returns a node's ID.\n *\n * @param attributes\n */\nexport function getNodeId({ attributes }: UiNode) {\n  if (isUiNodeInputAttributes(attributes)) {\n    return attributes.name\n  } else {\n    return attributes.id\n  }\n}\n\n/**\n * Return the node input attribute type\n * In <input> elements we have a variety of types, such as text, password, email, etc.\n * When the attribute is null or the `type` attribute is not present, we assume it has no defined type.\n * @param attr\n * @returns type of node\n */\nexport const getNodeInputType = (attr: any): string => attr?.[\"type\"] ?? \"\"\n\nexport type FilterNodesByGroups = {\n  nodes: Array<UiNode>\n  groups?: Array<UiNodeGroupEnum | string> | UiNodeGroupEnum | string\n  withoutDefaultGroup?: boolean\n  attributes?:\n    | Array<UiNodeInputAttributesTypeEnum | string>\n    | UiNodeInputAttributesTypeEnum\n    | string\n  withoutDefaultAttributes?: boolean\n  excludeAttributes?:\n    | Array<UiNodeInputAttributesTypeEnum | string>\n    | UiNodeInputAttributesTypeEnum\n    | string\n}\n\n/**\n * Filters nodes by their groups and attributes.\n * If no filtering options are specified, all nodes are returned.\n * Will always add default nodes unless `withoutDefaultGroup` is true.\n * Will always add default attributes unless `withoutDefaultAttributes` is true.\n * @param {Object} filterNodesByGroups - An object containing the nodes and the filtering options.\n * @param {Array<UiNode>} filterNodesByGroups.nodes - An array of nodes.\n * @param {Array<UiNodeGroupEnum | string> | string} filterNodesByGroups.groups - An array or comma seperated strings of groups to filter by.\n * @param {boolean} filterNodesByGroups.withoutDefaultGroup - If true, will not add default nodes under the 'default' category.\n * @param {Array<UiNodeInputAttributesTypeEnum | string> | string} filterNodesByGroups.attributes - An array or comma seperated strings of attributes to filter by.\n * @param {boolean} filterNodesByGroups.withoutDefaultAttributes - If true, will not add default attributes such as 'hidden' and 'script'.\n */\nexport const filterNodesByGroups = ({\n  nodes,\n  groups,\n  withoutDefaultGroup,\n  attributes,\n  withoutDefaultAttributes,\n  excludeAttributes,\n}: FilterNodesByGroups) => {\n  const search = (s: Array<string> | string) =>\n    typeof s === \"string\" ? s.split(\",\") : s\n\n  return nodes.filter(({ group, attributes: attr }) => {\n    // if we have not specified any group or attribute filters, return all nodes\n    if (!groups && !attributes && !excludeAttributes) return true\n\n    const g = search(groups) || []\n    if (!withoutDefaultGroup) {\n      g.push(\"default\")\n    }\n\n    // filter the attributes\n    const a = search(attributes) || []\n    if (!withoutDefaultAttributes) {\n      // always add hidden fields e.g. csrf\n      if (group.includes(\"default\")) {\n        a.push(\"hidden\")\n      }\n      // automatically add the necessary fields for webauthn and totp\n      if (group.includes(\"webauthn\") || group.includes(\"totp\")) {\n        a.push(\"input\", \"script\")\n      }\n    }\n\n    // filter the attributes to exclude\n    const ea = search(excludeAttributes) || []\n\n    const filterGroup = groups ? g.includes(group) : true\n    const filterAttributes = attributes\n      ? a.includes(getNodeInputType(attr))\n      : true\n    const filterExcludeAttributes = excludeAttributes\n      ? !ea.includes(getNodeInputType(attr))\n      : true\n\n    return filterGroup && filterAttributes && filterExcludeAttributes\n  })\n}\n"],"names":[],"mappings":"AAkBa,MAAA,YAAA,GAAe,CAAC,IAAyB,KAAA;AACpD,EAAA,MAAM,aAAa,IAAK,CAAA,UAAA,CAAA;AACxB,EAAI,IAAA,wBAAA,CAAyB,UAAU,CAAG,EAAA;AACxC,IAAA,OAAO,WAAW,KAAM,CAAA,IAAA,CAAA;AAAA,GAC1B;AAEA,EAAI,IAAA,uBAAA,CAAwB,UAAU,CAAG,EAAA;AACvC,IAAO,OAAA,IAAA,CAAK,IAAK,CAAA,KAAA,EAAO,IAAQ,IAAA,EAAA,CAAA;AAAA,GAClC;AAEA,EAAI,IAAA,uBAAA,CAAwB,UAAU,CAAG,EAAA;AACvC,IAAI,IAAA,UAAA,CAAW,OAAO,IAAM,EAAA;AAC1B,MAAA,OAAO,WAAW,KAAM,CAAA,IAAA,CAAA;AAAA,KAC1B;AAAA,GACF;AAEA,EAAO,OAAA,IAAA,CAAK,I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