{"version":3,"file":"combobox-utils.mjs","names":[],"sources":["../../../src/combobox/combobox-utils.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport {\n  type ComboboxAction,\n  type ComboboxFilterOption,\n  type ComboboxOption,\n  type ComboboxOptionGroup,\n} from './combobox-types';\n\ntype ComboboxOptions<TOption extends ComboboxOption> =\n  | TOption[]\n  | ComboboxOptionGroup<TOption>[];\n\nconst isOptionGroup = <TOption extends ComboboxOption>(\n  item: TOption | ComboboxOptionGroup<TOption>,\n): item is ComboboxOptionGroup<TOption> => 'options' in item;\n\nconst matchesLabel = (option: ComboboxOption, searchValue: string) =>\n  option.label.toLowerCase().includes(searchValue.toLowerCase());\n\n/**\n * Filters options against the search value, keeping the grouped or flat shape\n * of the input. Groups that end up without matches are dropped.\n */\nconst filterComboboxOptions = <TOption extends ComboboxOption>(\n  options: ComboboxOptions<TOption>,\n  searchValue: string,\n  filterOption?: ComboboxFilterOption<TOption>,\n): ComboboxOptions<TOption> => {\n  if (filterOption === false || searchValue.trim() === '') {\n    return options;\n  }\n\n  const matches = filterOption ?? matchesLabel;\n\n  if (options.every(isOptionGroup)) {\n    return options\n      .map((group) => ({\n        ...group,\n        options: group.options.filter((option) => matches(option, searchValue)),\n      }))\n      .filter((group) => group.options.length > 0);\n  }\n\n  return (options as TOption[]).filter((option) =>\n    matches(option, searchValue),\n  );\n};\n\n/** Flattens grouped options into the visual order used for option ids. */\nconst flattenComboboxOptions = <TOption extends ComboboxOption>(\n  options: (TOption | ComboboxOptionGroup<TOption>)[],\n): TOption[] =>\n  options.flatMap((item) => (isOptionGroup(item) ? item.options : item));\n\n// A symbol keeps the marker out of consumer data and out of anything that walks\n// option keys, so the create row travels through the list as a normal option.\nconst CREATE_MARKER = Symbol('ndlComboboxCreateOption');\n\ntype ComboboxCreateOption<TOption extends ComboboxOption> = TOption & {\n  readonly [CREATE_MARKER]: true;\n};\n\nconst isCreateOption = <TOption extends ComboboxOption>(\n  option: TOption,\n): option is ComboboxCreateOption<TOption> => CREATE_MARKER in option;\n\n/** Tags an option as the create row, so activating it creates instead of selects. */\nconst asCreateOption = <TOption extends ComboboxOption>(\n  option: TOption,\n): ComboboxCreateOption<TOption> => ({\n  ...option,\n  [CREATE_MARKER]: true,\n});\n\n/**\n * The default way to build the option to create. It can only fill in the fields\n * of `ComboboxOption`, so an option type with more required fields of its own\n * has to supply `createOption`.\n */\nconst buildCreateOption = <TOption extends ComboboxOption>(\n  searchValue: string,\n): TOption =>\n  ({\n    label: searchValue,\n    value: searchValue,\n  }) as TOption;\n\nconst formatCreateOptionLabel = (searchValue: string) =>\n  `Create \"${searchValue}\"`;\n\n// Actions travel through the list as rows, which is what gives them virtual\n// focus, an option id and keyboard activation. They cannot be options, since an\n// option type with fields of its own is the consumer's to build, so they are\n// their own row shape marked the same way as the create row.\nconst ACTION_MARKER = Symbol('ndlComboboxAction');\n\ntype ComboboxActionRow = ComboboxOption & {\n  readonly [ACTION_MARKER]: ComboboxAction;\n};\n\n/** A row of the rendered list, which is either an option or an action. */\ntype ComboboxRow<TOption extends ComboboxOption> = TOption | ComboboxActionRow;\n\nconst isActionRow = <TOption extends ComboboxOption>(\n  row: ComboboxRow<TOption>,\n): row is ComboboxActionRow => ACTION_MARKER in row;\n\n/**\n * Wraps an action as a list row. The position in `actions` gives the row a\n * stable value, which keeps the active row from being reset every render.\n */\nconst asActionRow = (\n  action: ComboboxAction,\n  index: number,\n): ComboboxActionRow => ({\n  isDisabled: action.isDisabled,\n  label: action.label,\n  value: `ndl-combobox-action-${index}`,\n  [ACTION_MARKER]: action,\n});\n\nconst getAction = (row: ComboboxActionRow) => row[ACTION_MARKER];\n\nconst matchesExactly = (option: ComboboxOption, searchValue: string) => {\n  const normalized = searchValue.trim().toLowerCase();\n\n  return (\n    option.label.toLowerCase() === normalized ||\n    option.value.toLowerCase() === normalized\n  );\n};\n\n/**\n * A created option is never added to `options`, so the current selection is\n * checked too. Without that, retyping a created value would offer to create it\n * again and toggle the existing selection back off in multi select.\n */\nconst isValidNewComboboxOption = (\n  searchValue: string,\n  options: ComboboxOption[],\n) =>\n  searchValue.trim() !== '' &&\n  !options.some((option) => matchesExactly(option, searchValue));\n\n/**\n * Disabled options are deliberately included in keyboard navigation, following\n * the APG guidance on the focusability of disabled controls: skipping them\n * would make them undiscoverable to screen reader users. Activation is what\n * ignores them instead.\n */\nconst getFirstOptionIndex = (options: ComboboxOption[]) =>\n  options.length > 0 ? 0 : -1;\n\nconst getLastOptionIndex = (options: ComboboxOption[]) => options.length - 1;\n\n/**\n * Steps one option in the given direction, clamping at the ends of the list\n * as the combobox pattern permits.\n */\nconst getNextOptionIndex = (\n  options: ComboboxOption[],\n  fromIndex: number,\n  direction: 1 | -1,\n) => Math.min(Math.max(fromIndex + direction, 0), options.length - 1);\n\ntype ComboboxValue<TOption extends ComboboxOption> = TOption | TOption[] | null;\n\nconst getInitialValue = <TOption extends ComboboxOption>(\n  value: ComboboxValue<TOption> | undefined,\n  defaultValue: ComboboxValue<TOption> | undefined,\n  isMulti: boolean,\n): ComboboxValue<TOption> => {\n  if (value !== undefined) {\n    return value;\n  }\n\n  if (defaultValue !== undefined) {\n    return defaultValue;\n  }\n\n  if (isMulti) {\n    return [];\n  }\n\n  return null;\n};\n\nexport {\n  asActionRow,\n  asCreateOption,\n  buildCreateOption,\n  filterComboboxOptions,\n  flattenComboboxOptions,\n  formatCreateOptionLabel,\n  getAction,\n  getFirstOptionIndex,\n  getLastOptionIndex,\n  getNextOptionIndex,\n  isActionRow,\n  isCreateOption,\n  isOptionGroup,\n  isValidNewComboboxOption,\n  getInitialValue,\n};\nexport type { ComboboxActionRow, ComboboxRow, ComboboxValue };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAgCA,MAAM,iBACJ,SACyC,aAAa;AAExD,MAAM,gBAAgB,QAAwB,gBAC5C,OAAO,MAAM,YAAY,CAAC,CAAC,SAAS,YAAY,YAAY,CAAC;;;;;AAM/D,MAAM,yBACJ,SACA,aACA,iBAC6B;CAC7B,IAAI,iBAAiB,SAAS,YAAY,KAAK,MAAM,IACnD,OAAO;CAGT,MAAM,UAAU,iBAAA,QAAA,iBAAA,KAAA,IAAA,eAAgB;CAEhC,IAAI,QAAQ,MAAM,aAAa,GAC7B,OAAO,QACJ,KAAK,UAAA,eAAA,eAAA,CAAA,GACD,KAAA,GAAA,CAAA,GAAA,EACH,SAAS,MAAM,QAAQ,QAAQ,WAAW,QAAQ,QAAQ,WAAW,CAAC,EAAA,CACxE,CAAE,CAAC,CACF,QAAQ,UAAU,MAAM,QAAQ,SAAS,CAAC;CAG/C,OAAQ,QAAsB,QAAQ,WACpC,QAAQ,QAAQ,WAAW,CAC7B;AACF;;AAGA,MAAM,0BACJ,YAEA,QAAQ,SAAS,SAAU,cAAc,IAAI,IAAI,KAAK,UAAU,IAAK;AAIvE,MAAM,gBAAgB,OAAO,yBAAyB;AAMtD,MAAM,kBACJ,WAC4C,iBAAiB;;AAG/D,MAAM,kBACJ,WAAA,eAAA,eAAA,CAAA,GAEG,MAAA,GAAA,CAAA,GAAA,GACF,gBAAgB,KAAA,CACnB;;;;;;AAOA,MAAM,qBACJ,iBAEC;CACC,OAAO;CACP,OAAO;AACT;AAEF,MAAM,2BAA2B,gBAC/B,WAAW,YAAY;AAMzB,MAAM,gBAAgB,OAAO,mBAAmB;AAShD,MAAM,eACJ,QAC6B,iBAAiB;;;;;AAMhD,MAAM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