{"version":3,"file":"qualcomm-ui-angular-core-collection.mjs","sources":["../../src/collection/use-list-collection.ts","../../src/collection/qualcomm-ui-angular-core-collection.ts"],"sourcesContent":["// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.\n// SPDX-License-Identifier: BSD-3-Clause-Clear\n\nimport {\n  computed,\n  linkedSignal,\n  type Signal,\n  type WritableSignal,\n} from \"@angular/core\"\n\nimport {useFilter} from \"@qualcomm-ui/angular-core/locale\"\nimport {accessSignal} from \"@qualcomm-ui/angular-core/signals\"\nimport {\n  ListCollection,\n  type ListCollectionOptions,\n} from \"@qualcomm-ui/utils/collection\"\nimport type {FilterReturn} from \"@qualcomm-ui/utils/i18n\"\n\nexport interface UseListCollectionProps<T> extends Omit<\n  ListCollectionOptions<T>,\n  \"items\"\n> {\n  /**\n   * Filter strategy for the items. Use one of the predefined options, or pass as a\n   * function for full control.\n   *\n   * @option `contains` - Checks if string contains substring using locale-aware exact matching\n   * @option `endsWith` - Checks if string ends with substring using locale-aware exact matching\n   * @option `startsWith` - Checks if string starts with substring using locale-aware exact matching\n   * @option `fuzzyContains` - Checks if string contains substring within Levenshtein distance threshold\n   */\n  filter:\n    | keyof FilterReturn\n    | ((itemText: string, filterText: string, item: T) => boolean)\n  /**\n   * Optional filter text which will filter the items.\n   */\n  filterText?: Signal<string>\n  /**\n   * The items to use in the collection, prior to filtering.\n   */\n  items: Signal<T[]> | T[]\n  limit?: number\n}\n\n/**\n * @returns an object containing the mutable collection and filterText\n */\nexport function useListCollection<T>(\n  props: UseListCollectionProps<T>,\n): UseListCollectionReturn<T> {\n  const {\n    filter: filterProp,\n    filterText: filterTextProp,\n    items: itemsProp,\n    limit,\n    ...collectionOptions\n  } = props\n\n  let filterFn: Signal<\n    ((itemText: string, filterText: string, item: T) => boolean) | undefined\n  >\n  if (typeof filterProp === \"string\") {\n    const hook = useFilter({sensitivity: \"base\"})\n    filterFn = computed(() => hook()[filterProp])\n  } else {\n    filterFn = computed(() => filterProp)\n  }\n\n  const baseCollection = linkedSignal(\n    () =>\n      new ListCollection({\n        ...collectionOptions,\n        items: accessSignal(itemsProp) ?? [],\n      }),\n  )\n  const filterText = linkedSignal<string>(() => filterTextProp?.() || \"\")\n\n  const collection = computed(() => {\n    let result = baseCollection()\n    const filter = filterFn()\n\n    if (filterText() && filter) {\n      result = result.filter((itemString: string, _index: number, item: T) =>\n        filter(itemString, filterText(), item),\n      )\n    }\n\n    if (limit != null) {\n      result = new ListCollection({\n        ...collectionOptions,\n        items: result.items.slice(0, limit),\n      })\n    }\n\n    return result\n  })\n\n  return {\n    collection,\n    filterText,\n    reset: () => {\n      baseCollection.set(\n        new ListCollection({\n          ...collectionOptions,\n          items: accessSignal(itemsProp) ?? [],\n        }),\n      )\n    },\n  }\n}\n\nexport interface UseListCollectionReturn<T> {\n  /**\n   * The filtered collection. Pass this as input to your select/combobox.\n   */\n  collection: Signal<ListCollection<T>>\n  /**\n   * Mutable filter text which will filter the items when set. This may also be\n   * passed as an input property to this hook.\n   */\n  filterText: WritableSignal<string>\n  /**\n   * Helper function to reset the collection to its initial state.\n   */\n  reset: () => void\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;AAAA;AACA;AA4CA;;AAEG;AACG,SAAU,iBAAiB,CAC/B,KAAgC,EAAA;IAEhC,MAAM,EACJ,MAAM,EAAE,UAAU,EAClB,UAAU,EAAE,cAAc,EAC1B,KAAK,EAAE,SAAS,EAChB,KAAK,EACL,GAAG,iBAAiB,EACrB,GAAG,KAAK;AAET,IAAA,IAAI,QAEH;AACD,IAAA,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QAClC,MAAM,IAAI,GAAG,SAAS,CAAC,EAAC,WAAW,EAAE,MAAM,EAAC,CAAC;AAC7C,QAAA,QAAQ,GAAG,QAAQ,CAAC,MAAM,IAAI,EAAE,CAAC,UAAU,CAAC,CAAC;IAC/C;SAAO;QACL,QAAQ,GAAG,QAAQ,CAAC,MAA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