{"version":3,"file":"ListSearch.mjs","names":[],"sources":["../../../../../../../../components-base/src/list/search/ListSearch.ts"],"sourcesContent":["import type { ListModelContext } from \"../ListModelContext\";\nimport type { ListSearchShape, SearchValue } from \"./types\";\nimport type { InitialTableState } from \"@tanstack/table-core\";\n\n/**\n * The list's search term: where it starts, where it is kept, who to tell.\n *\n * The term itself lives in the table's global filter — this owns the initial\n * value, the persistence and the notification, none of which is a framework's\n * business. What a binding adds on top is how the field is _rendered_.\n */\nexport class ListSearch<T> {\n  public readonly context: ListModelContext<T>;\n  private readonly onUpdateCallbacks = new Set<() => unknown>();\n  private readonly initialValue?: string;\n  private readonly defaultValue?: string;\n  private readonly autosave: boolean;\n\n  public constructor(context: ListModelContext<T>, shape: ListSearchShape) {\n    const {\n      autosave = context.settingsDefaults?.search?.autosave ?? false,\n      defaultValue,\n    } = shape;\n\n    this.autosave = autosave;\n    this.context = context;\n    this.defaultValue = defaultValue;\n    this.initialValue = this.getInitialValue();\n  }\n\n  public get value(): SearchValue {\n    return this.context.dataTable.getState().globalFilter as SearchValue;\n  }\n\n  public get isSet(): boolean {\n    return this.value !== undefined;\n  }\n\n  private callOnUpdateCallbacks(): void {\n    this.onUpdateCallbacks.forEach((cb) => cb());\n  }\n\n  private getInitialValue(): string | undefined {\n    return (\n      this.context.settings?.get(\"search\", { autosave: this.autosave })\n        ?.value ?? this.defaultValue\n    );\n  }\n\n  public updateInitialState(initialState: InitialTableState): void {\n    initialState.globalFilter = this.initialValue;\n  }\n\n  public setValue(value: SearchValue): void {\n    const table = this.context.dataTable;\n\n    if (value === undefined || value.trim() === \"\") {\n      /*\n       * Only when something was actually set: clearing an empty search would\n       * otherwise reset the loader and refetch on every keystroke that leaves\n       * the field empty.\n       */\n      if (table.getState().globalFilter) {\n        table.setGlobalFilter(undefined);\n        this.callOnUpdateCallbacks();\n      }\n    } else {\n      table.setGlobalFilter(value);\n      this.callOnUpdateCallbacks();\n    }\n\n    this.context.settings?.store(\n      \"search\",\n      { value },\n      { autosave: this.autosave },\n    );\n  }\n\n  public clear(): void {\n    this.setValue(undefined);\n  }\n\n  public onUpdated(cb: () => unknown): void {\n    this.onUpdateCallbacks.add(cb);\n  }\n}\n\nexport default ListSearch;\n"],"mappings":";;;;;;;;;;AAWA,IAAa,aAAb,MAA2B;CACzB;CACA,oCAAqC,IAAI,IAAmB;CAC5D;CACA;CACA;CAEA,YAAmB,SAA8B,OAAwB;EACvE,MAAM,EACJ,WAAW,QAAQ,kBAAkB,QAAQ,YAAY,OACzD,iBACE;EAEJ,KAAK,WAAW;EAChB,KAAK,UAAU;EACf,KAAK,eAAe;EACpB,KAAK,eAAe,KAAK,gBAAgB;CAC3C;CAEA,IAAW,QAAqB;EAC9B,OAAO,KAAK,QAAQ,UAAU,SAAS,CAAC,CAAC;CAC3C;CAEA,IAAW,QAAiB;EAC1B,OAAO,KAAK,UAAU,KAAA;CACxB;CAEA,wBAAsC;EACpC,KAAK,kBAAkB,SAAS,OAAO,GAAG,CAAC;CAC7C;CAEA,kBAA8C;EAC5C,OACE,KAAK,QAAQ,UAAU,IAAI,UAAU,EAAE,UAAU,KAAK,SAAS,CAAC,CAAC,EAC7D,SAAS,KAAK;CAEtB;CAEA,mBAA0B,cAAuC;EAC/D,aAAa,eAAe,KAAK;CACnC;CAEA,SAAgB,OAA0B;EACxC,MAAM,QAAQ,KAAK,QAAQ;EAE3B,IAAI,UAAU,KAAA,KAAa,MAAM,KAAK,MAAM,IAMtC;OAAA,MAAM,SAAS,CAAC,CAAC,cAAc;IACjC,MAAM,gBAAgB,KAAA,CAAS;IAC/B,KAAK,sBAAsB;GAC7B;SACK;GACL,MAAM,gBAAgB,KAAK;GAC3B,KAAK,sBAAsB;EAC7B;EAEA,KAAK,QAAQ,UAAU,MACrB,UACA,EAAE,MAAM,GACR,EAAE,UAAU,KAAK,SAAS,CAC5B;CACF;CAEA,QAAqB;EACnB,KAAK,SAAS,KAAA,CAAS;CACzB;CAEA,UAAiB,IAAyB;EACxC,KAAK,kBAAkB,IAAI,EAAE;CAC/B;AACF"}