@hitgrab/finder 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +111 -159
- package/dist/index.d.mts +111 -159
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`react/jsx-runtime`),n=require(`lodash`);function r(e){return e}function i(e){return e}function a(e){return e}function o(e){return e}function s(e){return e}function c(e,t){return{rules:e,onChange:t}}function l(e,t){return{haystack:e,onChange:t}}function u(e){let t={...e};return delete t.boolean,delete t.multiple,t}function d(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function f(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const p=(0,e.createContext)(null);function m(){let t=(0,e.useContext)(p);if(t===null)throw Error(`useFinder requires a FinderContext.`);let[n]=t;return n}function h({children:e}){let n=m();return n.state===`loading`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function g({children:e}){let n=m();return n.state===`empty`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function _({children:e}){let n=m();return n.state===`noMatches`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function v({children:e}){let n=m();return n.state===`items`&&n.matches.items?typeof e==`function`?(0,t.jsx)(e,{items:n.matches.items,pagination:n.pagination,context:n.context}):e:null}function y({children:e}){let n=m();return n.state===`groups`&&n.matches.groups?typeof e==`function`?(0,t.jsx)(e,{groups:n.matches.groups,pagination:n.pagination,context:n.context}):e:null}function b({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&(0,t.jsx)(h,{children:e.loading},`loading`),e.empty&&(0,t.jsx)(g,{children:e.empty},`empty`),e.noMatches&&(0,t.jsx)(_,{children:e.noMatches},`noMatches`),e.items&&(0,t.jsx)(v,{children:e.items},`items`),e.groups&&(0,t.jsx)(y,{children:e.groups},`groups`)]}b.Loading=h,b.Empty=g,b.NoMatches=_,b.Items=v,b.Groups=y;function x(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function S(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=C(e,x(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=w(e,x(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function C(e,t){if(e.length<t.length)return;let r=e.indexOf(t);if(r!==-1)return(0,n.range)(r,r+t.length).map(e=>e)}function w(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var T=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function E(e,t){return(Array.isArray(e)?e:[e]).map(e=>new T(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=S(n.transformed,t);return r===void 0?e:k(O(n,D(r,n.transformed)))},void 0)}function D(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function O(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function k(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function ee(e,t){return(Array.isArray(e)?e:[e]).some(e=>S(x(e),t)!==void 0)}function A({needle:n,haystack:r,Match:i=`mark`,Miss:a}){let o=(0,e.useMemo)(()=>E(r,n),[r,n]);return o===void 0?r:o.map((e,n)=>{let r=[e.value,n].join();return e.is_match?typeof i==`string`?(0,t.jsx)(i,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(i,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r):a===void 0?e.value:typeof a==`string`?(0,t.jsx)(a,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(a,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r)})}function j({Match:e=`mark`,Miss:n,children:r}){let i=m();return i.search.hasSearchTerm===!1?r:(0,t.jsx)(A,{needle:i.search.searchTerm,haystack:r,Match:e,Miss:n})}function te(){let e=new Map;return(t,r)=>t.debounceMilliseconds===void 0?r():(e.has(t)===!1&&e.set(t,(0,n.debounce)(e=>e(),t.debounceMilliseconds)),e.get(t)?.(r))}var M=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const N={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},P={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},F={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var I=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function L(e){return typeof e==`object`&&!!e&&`sortFn`in e}function R(e){return typeof e==`object`&&!!e&&`searchFn`in e}function z(e){return typeof e==`object`&&!!e&&`filterFn`in e}function B(e){return z(e)&&typeof e.options!=`function`}function V(e){return typeof e==`object`&&!!e&&`groupFn`in e}function ne(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function re(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function ie(e){return z(e)&&e.boolean===!0}function ae(e){return z(e)&&e.multiple===!0}function oe(e){return z(e)&&e.multiple!==!0&&e.boolean!==!0}function H(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function U(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new I(F.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new I(F.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new I(F.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new I(F.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function W(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function G(e){if(ae(e))return U(e);if(ie(e))return H(e);if(oe(e))return W(e);throw new I(F.INVALID_RULE_SHAPE,{definition:e})}var K=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&G(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:P.FILTERS,event:N.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(B)}get activeRules(){return this.rules.filter(e=>G(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return G(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return G(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!B(t))throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,G(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,G(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new I(F.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=G(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:P.FILTERS,event:N.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return G(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=(0,n.uniqBy)([...this.rules,...e.rules],`id`),r={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:r}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new I(F.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>B(t)&&G(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>B(r)&&G(r).isMatch(t,e.values[r.id],n)))}},q=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(V(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(V)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,r){let i=t.reduce((t,n)=>{let i=e.rule?.groupFn(n,r);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(n)}),t},{}),a=Object.entries(i).map(([e,t])=>({id:e,items:t})),o=e.rule?.sticky!==void 0,s=[],c=[];if(o&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(a,r):e.rule.sticky;t&&(s.push(se(t)),c.push(`asc`))}return e.rule?.sortGroupFn&&(s.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,r)),c.push(e.groupBySortDirection??`asc`)),s.length>0?(0,n.orderBy)(a,s,c):a}};function se({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var J=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:P.PAGINATION,event:N.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:P.PAGINATION,event:N.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?(0,n.clamp)(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?((0,n.clamp)(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let r=Math.ceil(t.length/e.numItemsPerPage),i=((0,n.clamp)(e.page,1,r)-1)*e.numItemsPerPage;return t.slice(i,i+e.numItemsPerPage)}};function ce(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function le(e,t,r){let i=t.reduce((t,i)=>{if(e.rule?.searchFn===void 0)return t;let a=e.rule.searchFn(i,r),o=(Array.isArray(a)?a.map(x):[x(a)]).reduce((t,n)=>{let r=S(n,e.searchTerm);return r!==void 0&&t.push(ce(r,n)),t},[]);if(o.length>0){let e=(0,n.orderBy)(o,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:i,score:e})}return t},[]),a=i.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return(0,n.orderBy)(i,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/a*100],[`desc`]).map(e=>e.item)}var Y=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(R)===void 0)throw new I(F.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(R)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}setSearchTerm(e){let t=this.rule;if(!t)throw new I(F.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new I(F.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:P.SEARCH,event:N.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:P.SEARCH,event:N.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new I(F.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:le(e,t,n)}};const X=[void 0,`desc`,`asc`];var Z=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(L(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(L)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=X.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(X.length-1);this.setSortDirection(X[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,r)),e.sortDirection)}},Q=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?J.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=q.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=Y.process(e.search,r,n)),e.filters&&(r=K.process(e.filters,r,n)),e.sortBy&&(r=Z.process(e.sortBy,r,n)),r}},ue=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},de=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(z(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new I(F.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[R,z,L,V],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!R(e))throw new I(F.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new I(F.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new I(F.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},fe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{ee(n.haystack,e)&&n.onChange(t,e)})}},pe=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new de(e),this.ruleEffects=new ue(t),this.searchEffects=new fe(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){R(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},me=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new M,this.#i=new Q,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(ne)??[],C=n?.filter(re)??[];this.#a=new pe({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:te()};this.search=new Y({initialSearchTerm:r},w),this.filters=new K({initialFilters:c},w),this.sortBy=new Z({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new q({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new J({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(N.CHANGE,b),y&&this.#r.on(N.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(N.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(N.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:P.CORE,event:N.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(N.READY,{source:P.CORE,event:N.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Q.test({mixins:t,items:this.items,context:this.context})}return Q.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if((0,n.isEqual)(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:P.CORE,event:N.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:P.CORE,event:N.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){(0,n.isEqual)(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;(0,n.isEqual)(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},he=class{#e;constructor(e,t){let n=()=>this;this.#e=new me(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:n,rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S,controllerRef:C,children:w}){let[T,E]=(0,e.useState)(Date.now()),[D]=(0,e.useState)(()=>{let e=new he(n,{rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S});return e.events.on(N.SYNC_UPDATED_AT,e=>E(e)),e});D.setItems(n),D.setIsLoading(f),D.setIsDisabled(m),D.setRules(r),d!==void 0&&D.setContext(d),h!==void 0&&D.pagination.setPage(h),g!==void 0&&D.pagination.setNumItemsPerPage(g),(0,e.useImperativeHandle)(C,()=>D,[D]);let O=(0,e.useMemo)(()=>[D,T],[D,T]);return(0,t.jsx)(p.Provider,{value:O,children:w})}$.Content=b,$.SearchTermHaystack=j;function ge(){return(0,e.useRef)(null)}exports.Finder=$,exports.StringMatch=A,exports.filterRule=a,exports.finderRuleset=r,exports.groupByRule=s,exports.ruleEffect=c,exports.searchEffect=l,exports.searchRule=i,exports.sortByRule=o,exports.transformFilterToBoolean=d,exports.transformFilterToMultiple=f,exports.transformFilterToSingleValue=u,exports.useFinder=m,exports.useFinderRef=ge;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`react/jsx-runtime`),n=require(`lodash`);function r(e){return e}function i(e){return e}function a(e){return e}function o(e){return e}function s(e){return e}function c(e,t){return{rules:e,onChange:t}}function l(e){let t={...e};return delete t.boolean,delete t.multiple,t}function u(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function d(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const f=(0,e.createContext)(null);function p(){let t=(0,e.useContext)(f);if(t===null)throw Error(`useFinder requires a FinderContext.`);let[n]=t;return n}function m(e){let n=p();if(Object.values(e).length===0)throw Error(`No render props were found.`);let{loading:r,empty:i,noMatches:a,items:o,groups:s}=e;return n.state===`loading`?typeof r==`function`?(0,t.jsx)(r,{pagination:n.pagination,context:n.context}):r??null:n.state===`empty`?typeof i==`function`?(0,t.jsx)(i,{pagination:n.pagination,context:n.context}):i??null:n.state===`noMatches`?typeof a==`function`?(0,t.jsx)(a,{pagination:n.pagination,context:n.context}):a??null:n.state===`items`&&n.matches.items?typeof o==`function`?(0,t.jsx)(o,{items:n.matches.items,pagination:n.pagination,context:n.context}):o??null:n.state===`groups`&&n.matches.groups?typeof s==`function`?(0,t.jsx)(s,{groups:n.matches.groups,pagination:n.pagination,context:n.context}):s??null:null}function h(e,t,n){let r=new RegExp(/"(.*?)"/g),i=t,a=[],o,s=!1;for(;(o=r.exec(t))!==null&&s===!1;){let t=n(String(o[1])),r=g(e.transformed,t);r===void 0?s=!0:(a=a.concat(r),i=i.replace(o[0],``))}if(s)return;let c=n(i),l=_(e.transformed,c);if(l!==void 0)return a=a.concat(l),a.sort((e,t)=>e-t)}function g(e,t){if(e.length<t.length)return;let r=e.indexOf(t);if(r!==-1)return(0,n.range)(r,r+t.length).map(e=>e)}function _(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}function v(e,t,n){let r=h(e,t,n);if(r!==void 0)return x(b(e,y(r,e.transformed)))}function y(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function b(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.raw.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.raw.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.raw.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.raw.length){let t=a+s.length,r=e.raw.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function x(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}var S=class e{#e;constructor(t){this.raw=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}},C=class{#e;#t;#n;constructor(e,t){this.needle=t,this.haystack=new S(e)}get characterIndexes(){return this.#e===void 0&&(this.#e=h(this.haystack,this.needle,this.#r)),this.#e}get hasMatch(){return this.characterIndexes!==void 0}get score(){if(this.#t===void 0&&this.characterIndexes!==void 0){let e=this.characterIndexes.length/this.haystack.raw.length,t=1,n=1,r=0;for(let e=0;e<this.characterIndexes.length;e+=1)r!==void 0&&this.characterIndexes.at(e)===r+1&&(n+=1,n>=t&&(t=n)),r=this.characterIndexes.at(e);this.#t={percentOfHaystackMatched:e,longestSequentialSequence:t}}return this.#t}get segments(){return this.#n===void 0&&(this.#n=v(this.haystack,this.needle,this.#r)),this.#n}#r(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}};function w({needle:n,haystack:r,Match:i=`mark`,Miss:a}){let o=(0,e.useMemo)(()=>new C(r,n),[r,n]);return o.hasMatch===!1||o.segments===void 0?r:o.segments.map((e,n)=>{let r=[e.value,n].join();return e.is_match?typeof i==`string`?(0,t.jsx)(i,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(i,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r):a===void 0?e.value:typeof a==`string`?(0,t.jsx)(a,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(a,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r)})}function T({Match:e=`mark`,Miss:n,children:r}){let i=p();return i.search.hasSearchTerm===!1?r:(0,t.jsx)(w,{needle:i.search.searchTerm,haystack:r,Match:e,Miss:n})}function E(){let e=new Map;return(t,r)=>t.debounceMilliseconds===void 0?r():(e.has(t)===!1&&e.set(t,(0,n.debounce)(e=>e(),t.debounceMilliseconds)),e.get(t)?.(r))}var D=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}get isSilenced(){return this.#t}};const O={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},k={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},A={INITIAL_RULE_NOT_FOUND:`Finder ignored an initial rule that could not be found.`,INITIAL_SEARCH_RULE_NOT_FOUND:`Finder ignored an initialSearchTerm, as no SearchRule was found.`,GROUP_SORT_DIRECTION_SET_WITHOUT_SORT_FN:`The active groupBy rule has a defined sort direction, but no corresponding sortGroupFn.`},j={INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`,RULE_NOT_FOUND:`Finder could not find requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search term must be a string.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`};var M=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function N(e){return typeof e==`object`&&!!e&&`sortFn`in e}function P(e){return typeof e==`object`&&!!e&&`searchFn`in e}function F(e){return typeof e==`object`&&!!e&&`filterFn`in e}function I(e){return F(e)&&typeof e.options!=`function`}function L(e){return typeof e==`object`&&!!e&&`groupFn`in e}function R(e){return F(e)&&e.boolean===!0}function z(e){return F(e)&&e.multiple===!0}function B(e){return F(e)&&e.multiple!==!0&&e.boolean!==!0}function V(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new M(j.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new M(j.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new M(j.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new M(j.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function H(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new M(j.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new M(j.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new M(j.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new M(j.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new M(j.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new M(j.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new M(j.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new M(j.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function U(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new M(j.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(t,n){if(n===void 0)return t!==void 0;let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return r!==void 0&&t===r.value},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new M(j.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new M(j.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function W(e){if(z(e))return H(e);if(R(e))return V(e);if(B(e))return U(e);throw new M(j.INVALID_RULE_SHAPE,{definition:e})}var G=class{#e=new Map;#t=new Map;#n;constructor({initialFilters:e},t){this.#n=t,e&&Object.entries(e).forEach(([e,t])=>{this.#n.getRuleBook().hasRule(e)?(this.#e.set(e,t),this.#t.set(e,t)):console.warn(A.INITIAL_RULE_NOT_FOUND,{rule:e})})}set(e,t){if(this.#n.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&W(n).validate(a),!(this.#e.has(n.id)&&this.#e.get(n.id)===a)&&this.#n.debouncer(n,()=>{let e=a===this.#t.has(n.id),t=a===n.defaultValue;e||t?this.#e.delete(n.id):this.#e.set(n.id,a),this.#n.touch({source:k.FILTERS,event:O.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#n.getRuleBook().rules.filter(I)}get activeRules(){return this.rules.filter(e=>W(e).isActive(this.#e.get(e.id)))}get touchedRules(){return this.rules.filter(e=>this.#e.has(e.id))}get(e){let t=this.getRule(e),n=this.#e.get(t.id);return W(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e.get(n.id);return W(n).has(r,t)}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(!I(t))throw new M(j.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e.get(n.id);this.set(n,W(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e.get(n.id);this.set(n,W(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e.get(n.id);if(n.boolean&&t!==void 0)throw new M(j.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=W(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e.clear(),this.#n.touch({source:k.FILTERS,event:O.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e.get(t.id);return W(t).isActive(n)}test(e){if(this.#n.isLoading())return[];if(e.isAdditive){let t=(0,n.uniqBy)([...this.rules,...e.rules],`id`),r={...this.values,...e.values};return this.#n.test({filters:{rules:t,values:r}},!0)}return this.#n.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#n.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new M(j.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return Object.fromEntries(this.#e.entries())}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,touchedRules:this.touchedRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>I(t)&&W(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>I(r)&&W(r).isMatch(t,e.values[r.id],n)))}},K=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#n.getRuleBook().hasRule(e)?this.#e=this.getRule(e):console.warn(A.INITIAL_RULE_NOT_FOUND,{initialGroupBy:e})),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(L(t)===!1)throw new M(j.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(L)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:k.GROUP_BY,event:O.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:k.GROUP_BY,event:O.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,r){let i=t.reduce((t,n)=>{let i=e.rule?.groupFn(n,r);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(n)}),t},{}),a=Object.entries(i).map(([e,t])=>({id:e,items:t})),o=e.rule?.sticky!==void 0,s=[],c=[];if(o&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(a,r):e.rule.sticky;t&&(s.push(ee(t)),c.push(`asc`))}return e.rule?.sortGroupFn&&(s.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,r)),c.push(e.groupBySortDirection??`asc`)),e.rule?.sortGroupFn===void 0&&e.groupBySortDirection!==void 0&&console.warn(A.GROUP_SORT_DIRECTION_SET_WITHOUT_SORT_FN,{rule:e.rule}),s.length>0?(0,n.orderBy)(a,s,c):a}};function ee({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var q=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==void 0&&e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:k.PAGINATION,event:O.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:k.PAGINATION,event:O.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?(0,n.clamp)(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?((0,n.clamp)(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let r=Math.ceil(t.length/e.numItemsPerPage),i=((0,n.clamp)(e.page,1,r)-1)*e.numItemsPerPage;return t.slice(i,i+e.numItemsPerPage)}};function J(e,t,r){let i=t.reduce((t,i)=>{if(e.rule?.searchFn===void 0)return t;let a=e.rule.searchFn(i,r),o=(Array.isArray(a)?a:[a]).map(t=>new C(t,e.searchTerm)).filter(e=>e.hasMatch).map(e=>e.score);if(o.length>0){let e=(0,n.orderBy)(o,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:i,score:e})}return t},[]),a=i.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return(0,n.orderBy)(i,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/a*100],[`desc`]).map(e=>e.item)}var Y=class{#e;constructor({initialSearchTerm:e},t){this.searchTerm=``,e&&t.getRuleBook().rules.find(P)===void 0&&console.warn(A.INITIAL_SEARCH_RULE_NOT_FOUND,{initialSearchTerm:e}),this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(P)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}get suggestedFilters(){let e=[],t=this.rule;if(this.hasSearchTerm&&t?.suggestFiltersFrom){let r=this.#e.getRuleBook().rules.filter(I),[i,a]=(0,n.partition)((Array.isArray(t.suggestFiltersFrom)?t.suggestFiltersFrom:[t.suggestFiltersFrom]).map(e=>{let t=r.find(t=>typeof e==`string`?t.id===e:e.id===t.id);if(t===void 0)throw new M(j.RULE_NOT_FOUND,{rule:t});return t}),e=>e.options!==void 0&&e.options.length>0);i.forEach(t=>{if(t.options&&t.options.length>0){let n=J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},t.options??[]);n.length>0&&e.push({rule:t,optionMatches:n})}}),a.length>0&&J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},a).forEach(t=>{e.push({rule:t})})}return e}setSearchTerm(e){let t=this.rule;if(!t)throw new M(j.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new M(j.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:k.SEARCH,event:O.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:k.SEARCH,event:O.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,suggestedFilters:this.suggestedFilters,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new M(j.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:J(e,t,n)}};const X=[void 0,`desc`,`asc`];var Z=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#n.getRuleBook().hasRule(e)?this.#e=this.getRule(e):console.warn(A.INITIAL_RULE_NOT_FOUND,{initialSortBy:e})),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(N(t)===!1)throw new M(j.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(N)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:k.SORT_BY,event:O.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=X.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(X.length-1);this.setSortDirection(X[t])}}toggleSortDirection(){if(this.sortDirection===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:k.SORT_BY,event:O.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,r)),e.sortDirection)}},Q=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?q.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=K.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=Y.process(e.search,r,n)),e.filters&&(r=G.process(e.filters,r,n)),e.sortBy&&(r=Z.process(e.sortBy,r,n)),r}},te=class{#e;constructor(e){this.#e=[],this.effects=[],e&&(this.#e=e)}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},ne=class e{constructor(t){this.definitions=[],this.rules=[],t&&(e.validateDefinitions(t),this.definitions=t)}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(F(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}hasRule(e){return this.rules.some(t=>typeof e==`object`?t.id===e.id:t.id===e)}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new M(j.RULE_NOT_FOUND,{identifier:e});return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[P,F,N,L],n=new Set;return e.forEach(e=>{if(e.id===void 0&&P(e)===!1)throw new M(j.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new M(j.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new M(j.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},re=class{constructor({rules:e,effects:t}){this.list=new ne(e),this.effects=new te(t)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.effects.hydrateDefinitions(e,t)}onChange(e,t){this.effects.onChange(e,t)}},ie=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new D,this.#i=new Q,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f,this.#a=new re({rules:t,effects:n}),this.#a.hydrateDefinitions(e??[],l);let S={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:E()};this.search=new Y({initialSearchTerm:r},S),this.filters=new G({initialFilters:c},S),this.sortBy=new Z({initialSortBy:i,initialSortDirection:a},S),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},S),this.pagination=new q({page:u,numItemsPerPage:d},S),this.#r.silently(()=>{let e={source:k.CORE,event:O.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(O.CHANGE,b),y&&this.#r.on(O.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:k.CORE,event:O.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(O.READY,v))}#o(e){if(this.#r.isSilenced)return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(O.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(O.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:k.CORE,event:O.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(O.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(O.READY,{source:k.CORE,event:O.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Q.test({mixins:t,items:this.items,context:this.context})}return Q.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilenced}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if((0,n.isEqual)(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:k.CORE,event:O.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:k.CORE,event:O.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:k.CORE,event:O.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){e!==void 0&&(0,n.isEqual)(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){if(e===void 0||Object.is(e,this.context))return;let t=this.context;(0,n.isEqual)(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:k.CORE,event:O.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},ae=class{#e;constructor(e,t){let n=()=>this;this.#e=new ie(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:n,rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:p,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S,controllerRef:C,children:w}){let[T,E]=(0,e.useState)(Date.now()),[D]=(0,e.useState)(()=>{let e=new ae(n,{rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:p,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S});return e.events.on(O.SYNC_UPDATED_AT,e=>E(e)),e});D.setItems(n),D.setIsLoading(p),D.setIsDisabled(m),D.setRules(r),D.setContext(d),D.pagination.setPage(h),D.pagination.setNumItemsPerPage(g),(0,e.useImperativeHandle)(C,()=>D,[D]);let k=(0,e.useMemo)(()=>[D,T],[D,T]);return(0,t.jsx)(f.Provider,{value:k,children:w})}$.Content=m,$.SearchTermHaystack=T;function oe(){return(0,e.useRef)(null)}exports.Finder=$,exports.StringMatch=w,exports.filterRule=a,exports.finderRuleset=r,exports.groupByRule=s,exports.ruleEffect=c,exports.searchRule=i,exports.sortByRule=o,exports.transformFilterToBoolean=u,exports.transformFilterToMultiple=d,exports.transformFilterToSingleValue=l,exports.useFinder=p,exports.useFinderRef=oe;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,93 +1,5 @@
|
|
|
1
1
|
import { ElementType, PropsWithChildren, ReactElement, ReactNode, RefObject } from "react";
|
|
2
2
|
|
|
3
|
-
//#region src/core/types/rule-types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Select a property from the item to sort by.
|
|
6
|
-
*/
|
|
7
|
-
type FinderPropertySelector<FItem, FContext = any> = (item: FItem, context: FContext) => string | number;
|
|
8
|
-
interface Rule {
|
|
9
|
-
id: string;
|
|
10
|
-
debounceMilliseconds?: number;
|
|
11
|
-
label?: string;
|
|
12
|
-
hidden?: boolean;
|
|
13
|
-
}
|
|
14
|
-
type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, FContext> | FilterRuleDefinition<FItem, FContext> | SortByRuleDefinition<FItem, FContext> | GroupByRuleDefinition<FItem, FContext>;
|
|
15
|
-
interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
|
|
16
|
-
id?: string;
|
|
17
|
-
searchFn?: (item: FItem, context: FContext) => string | string[];
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Describes the display of a filter or sort option.
|
|
21
|
-
*/
|
|
22
|
-
interface FilterOption<FValue = any> {
|
|
23
|
-
label?: string;
|
|
24
|
-
value: FValue;
|
|
25
|
-
disabled?: boolean;
|
|
26
|
-
}
|
|
27
|
-
interface FilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends Rule {
|
|
28
|
-
filterFn: (item: FItem, value: FValue, context: FContext) => boolean;
|
|
29
|
-
required?: boolean;
|
|
30
|
-
strictOptions?: boolean;
|
|
31
|
-
multiple?: boolean;
|
|
32
|
-
boolean?: boolean;
|
|
33
|
-
options?: FilterOption<FValue>[] | ((options: {
|
|
34
|
-
items: FItem[];
|
|
35
|
-
context: FContext;
|
|
36
|
-
}) => FilterOption<FValue>[]);
|
|
37
|
-
}
|
|
38
|
-
type AnyFilterRuleDefinition<FItem = any, FValue = any> = Omit<FilterRuleDefinition<FItem, FValue>, "options">;
|
|
39
|
-
interface FilterRuleWithBooleanValue<FItem = any, FValue = boolean, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
40
|
-
multiple?: false;
|
|
41
|
-
boolean: true;
|
|
42
|
-
defaultValue?: boolean;
|
|
43
|
-
options?: never;
|
|
44
|
-
}
|
|
45
|
-
interface FilterRuleWithSingleValue<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
46
|
-
multiple?: false;
|
|
47
|
-
boolean?: false;
|
|
48
|
-
defaultValue?: FValue;
|
|
49
|
-
}
|
|
50
|
-
interface FilterRuleWithMultipleValues<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
51
|
-
multiple: true;
|
|
52
|
-
boolean?: false;
|
|
53
|
-
defaultValue?: FValue[];
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* A hydrated filter has rendered any option generator functions, and narrowed ambiguous properties from FilterRule.
|
|
57
|
-
*/
|
|
58
|
-
interface HydratedFilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
59
|
-
options?: FilterOption<FValue>[];
|
|
60
|
-
multiple: boolean;
|
|
61
|
-
boolean: boolean;
|
|
62
|
-
_isHydrated: true;
|
|
63
|
-
}
|
|
64
|
-
interface SortByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
65
|
-
sortFn: FinderPropertySelector<FItem, FContext> | FinderPropertySelector<FItem, FContext>[];
|
|
66
|
-
defaultSortDirection?: SortDirection;
|
|
67
|
-
}
|
|
68
|
-
interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
69
|
-
groupFn: (item: FItem, context: FContext) => string | number | (string | number)[];
|
|
70
|
-
sortGroupFn?: FinderPropertySelector<FinderResultGroup<FItem>, FContext>;
|
|
71
|
-
defaultGroupSortDirection?: SortDirection;
|
|
72
|
-
sticky?: {
|
|
73
|
-
header?: string | string[];
|
|
74
|
-
footer?: string | string[];
|
|
75
|
-
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
76
|
-
header?: string | string[];
|
|
77
|
-
footer?: string | string[];
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
interface FilterTestOptions {
|
|
81
|
-
rules: HydratedFilterRuleDefinition[];
|
|
82
|
-
values?: Record<string, any>;
|
|
83
|
-
isAdditive?: boolean;
|
|
84
|
-
}
|
|
85
|
-
interface FilterTestRuleOptions {
|
|
86
|
-
rule: string | AnyFilterRuleDefinition;
|
|
87
|
-
value: any;
|
|
88
|
-
isAdditive?: boolean;
|
|
89
|
-
}
|
|
90
|
-
//#endregion
|
|
91
3
|
//#region src/core/core-constants.d.ts
|
|
92
4
|
declare const EVENTS: {
|
|
93
5
|
readonly INIT: "init";
|
|
@@ -189,6 +101,7 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
189
101
|
get search(): {
|
|
190
102
|
rule: SearchRuleDefinition<unknown, any> | undefined;
|
|
191
103
|
searchTerm: string;
|
|
104
|
+
suggestedFilters: SearchRuleSuggestion[];
|
|
192
105
|
hasSearchTerm: boolean;
|
|
193
106
|
hasSearchRule: boolean;
|
|
194
107
|
setSearchTerm: (value: string) => void;
|
|
@@ -197,8 +110,11 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
197
110
|
};
|
|
198
111
|
get filters(): {
|
|
199
112
|
values: Record<string, any>;
|
|
200
|
-
raw:
|
|
113
|
+
raw: {
|
|
114
|
+
[k: string]: any;
|
|
115
|
+
};
|
|
201
116
|
activeRules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
117
|
+
touchedRules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
202
118
|
rules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
203
119
|
isActive: (identifier: string | AnyFilterRuleDefinition<any, any>) => boolean;
|
|
204
120
|
get: (identifier: string | AnyFilterRuleDefinition<any, any>) => any;
|
|
@@ -247,7 +163,7 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
247
163
|
numTotalItems: number;
|
|
248
164
|
lastPage: number | undefined;
|
|
249
165
|
isPaginated: boolean;
|
|
250
|
-
setPage: (value
|
|
166
|
+
setPage: (value?: number) => void;
|
|
251
167
|
setNumItemsPerPage: (value?: number) => void;
|
|
252
168
|
};
|
|
253
169
|
/**
|
|
@@ -256,8 +172,8 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
256
172
|
setItems(items: FItem[] | null | undefined): void;
|
|
257
173
|
setIsLoading(value?: boolean): void;
|
|
258
174
|
setIsDisabled(value?: boolean): void;
|
|
259
|
-
setRules(definitions
|
|
260
|
-
setContext(context
|
|
175
|
+
setRules(definitions?: RuleDefinition<FItem>[]): void;
|
|
176
|
+
setContext(context?: FContext): void;
|
|
261
177
|
/**
|
|
262
178
|
* Utils
|
|
263
179
|
*/
|
|
@@ -266,20 +182,104 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
266
182
|
toJSON(): Omit<FinderConstructorOptions<FItem, any>, "rules">;
|
|
267
183
|
}
|
|
268
184
|
//#endregion
|
|
269
|
-
//#region src/core/types/
|
|
185
|
+
//#region src/core/types/rule-types.d.ts
|
|
186
|
+
/**
|
|
187
|
+
* Select a property from the item to sort by.
|
|
188
|
+
*/
|
|
189
|
+
type FinderPropertySelector<FItem, FContext = any> = (item: FItem, context: FContext) => string | number;
|
|
190
|
+
interface Rule {
|
|
191
|
+
id: string;
|
|
192
|
+
debounceMilliseconds?: number;
|
|
193
|
+
label?: string;
|
|
194
|
+
hidden?: boolean;
|
|
195
|
+
}
|
|
196
|
+
type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, FContext> | FilterRuleDefinition<FItem, FContext> | SortByRuleDefinition<FItem, FContext> | GroupByRuleDefinition<FItem, FContext>;
|
|
197
|
+
interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
|
|
198
|
+
id?: string;
|
|
199
|
+
searchFn?: (item: FItem, context: FContext) => string | string[];
|
|
200
|
+
suggestFiltersFrom?: string | AnyFilterRuleDefinition | (string | AnyFilterRuleDefinition)[];
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Describes the display of a filter or sort option.
|
|
204
|
+
*/
|
|
205
|
+
interface FilterOption<FValue = any> {
|
|
206
|
+
label?: string;
|
|
207
|
+
value: FValue;
|
|
208
|
+
disabled?: boolean;
|
|
209
|
+
}
|
|
210
|
+
interface FilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends Rule {
|
|
211
|
+
filterFn: (item: FItem, value: FValue, context: FContext) => boolean;
|
|
212
|
+
required?: boolean;
|
|
213
|
+
strictOptions?: boolean;
|
|
214
|
+
multiple?: boolean;
|
|
215
|
+
boolean?: boolean;
|
|
216
|
+
options?: FilterOption<FValue>[] | ((options: {
|
|
217
|
+
items: FItem[];
|
|
218
|
+
context: FContext;
|
|
219
|
+
}) => FilterOption<FValue>[]);
|
|
220
|
+
defaultValue?: any;
|
|
221
|
+
}
|
|
222
|
+
type AnyFilterRuleDefinition<FItem = any, FValue = any> = Omit<FilterRuleDefinition<FItem, FValue>, "options">;
|
|
223
|
+
interface FilterRuleWithBooleanValue<FItem = any, FValue = boolean, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
224
|
+
multiple?: false;
|
|
225
|
+
boolean: true;
|
|
226
|
+
defaultValue?: boolean;
|
|
227
|
+
options?: never;
|
|
228
|
+
}
|
|
229
|
+
interface FilterRuleWithSingleValue<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
230
|
+
multiple?: false;
|
|
231
|
+
boolean?: false;
|
|
232
|
+
defaultValue?: FValue;
|
|
233
|
+
}
|
|
234
|
+
interface FilterRuleWithMultipleValues<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
235
|
+
multiple: true;
|
|
236
|
+
boolean?: false;
|
|
237
|
+
defaultValue?: FValue[];
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* A hydrated filter has rendered any option generator functions, and narrowed ambiguous properties from FilterRule.
|
|
241
|
+
*/
|
|
242
|
+
interface HydratedFilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
243
|
+
options?: FilterOption<FValue>[];
|
|
244
|
+
multiple: boolean;
|
|
245
|
+
boolean: boolean;
|
|
246
|
+
_isHydrated: true;
|
|
247
|
+
}
|
|
248
|
+
interface SortByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
249
|
+
sortFn: FinderPropertySelector<FItem, FContext> | FinderPropertySelector<FItem, FContext>[];
|
|
250
|
+
defaultSortDirection?: SortDirection;
|
|
251
|
+
}
|
|
252
|
+
interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
253
|
+
groupFn: (item: FItem, context: FContext) => string | number | (string | number)[];
|
|
254
|
+
sortGroupFn?: FinderPropertySelector<FinderResultGroup<FItem>, FContext>;
|
|
255
|
+
defaultGroupSortDirection?: SortDirection;
|
|
256
|
+
sticky?: {
|
|
257
|
+
header?: string | string[];
|
|
258
|
+
footer?: string | string[];
|
|
259
|
+
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
260
|
+
header?: string | string[];
|
|
261
|
+
footer?: string | string[];
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
interface FilterTestOptions {
|
|
265
|
+
rules: HydratedFilterRuleDefinition[];
|
|
266
|
+
values?: Record<string, any>;
|
|
267
|
+
isAdditive?: boolean;
|
|
268
|
+
}
|
|
269
|
+
interface FilterTestRuleOptions {
|
|
270
|
+
rule: string | AnyFilterRuleDefinition;
|
|
271
|
+
value: any;
|
|
272
|
+
isAdditive?: boolean;
|
|
273
|
+
}
|
|
270
274
|
interface RuleEffect<FItem = any, FContext = any> {
|
|
271
275
|
rules: string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[] | ((items: FItem[], context: FContext) => string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[]);
|
|
272
276
|
onChange: (instance: FinderCore<FItem, FContext>, rule: RuleDefinition<FItem>) => void;
|
|
273
277
|
}
|
|
274
|
-
interface SearchEffect<FItem = any, FContext = any> {
|
|
275
|
-
haystack: string | string[] | ((items: FItem[], context: FContext) => string | string[]);
|
|
276
|
-
onChange: (instance: FinderCore<FItem, FContext>, searchTerm: string) => void;
|
|
277
|
-
}
|
|
278
278
|
//#endregion
|
|
279
279
|
//#region src/core/types/core-types.d.ts
|
|
280
280
|
interface FinderConstructorOptions<FItem, FContext = any> {
|
|
281
|
-
rules
|
|
282
|
-
effects?:
|
|
281
|
+
rules?: RuleDefinition<FItem>[];
|
|
282
|
+
effects?: RuleEffect[];
|
|
283
283
|
context?: FContext;
|
|
284
284
|
isLoading?: boolean;
|
|
285
285
|
disabled?: boolean;
|
|
@@ -349,6 +349,10 @@ interface PaginationMixinInterface {
|
|
|
349
349
|
setPage: (value: number) => void;
|
|
350
350
|
setNumItemsPerPage: (value: number) => void;
|
|
351
351
|
}
|
|
352
|
+
interface SearchRuleSuggestion {
|
|
353
|
+
rule: HydratedFilterRuleDefinition;
|
|
354
|
+
optionMatches?: FilterOption[];
|
|
355
|
+
}
|
|
352
356
|
//#endregion
|
|
353
357
|
//#region src/core/types/string-match-types.d.ts
|
|
354
358
|
/**
|
|
@@ -396,7 +400,7 @@ interface StringMatchSegmentProps {
|
|
|
396
400
|
}
|
|
397
401
|
type FinderSearchTermProp = keyof HTMLElementTagNameMap | ElementType | ElementType<StringMatchSegmentProps>;
|
|
398
402
|
//#endregion
|
|
399
|
-
//#region src/core/utils/rule-type-
|
|
403
|
+
//#region src/core/utils/rule-type-guards.d.ts
|
|
400
404
|
/**
|
|
401
405
|
* Enforce structure for an array of rule of mixed types.
|
|
402
406
|
*/
|
|
@@ -408,7 +412,6 @@ declare function filterRule<FItem, FValue = any, FContext = any, T = FilterRuleW
|
|
|
408
412
|
declare function sortByRule<FItem, FContext = any>(rule: SortByRuleDefinition<FItem, FContext>): SortByRuleDefinition<FItem, FContext>;
|
|
409
413
|
declare function groupByRule<FItem, FContext = any>(rule: GroupByRuleDefinition<FItem, FContext>): GroupByRuleDefinition<FItem, FContext>;
|
|
410
414
|
declare function ruleEffect<FItem, FContext = any>(rules: string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[] | ((items: FItem[], context: FContext) => string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[]), onChange: (instance: FinderCore<FItem, FContext>, rule: RuleDefinition) => void): RuleEffect<FItem, FContext>;
|
|
411
|
-
declare function searchEffect<FItem, FContext = any>(haystack: string | string[] | ((items: FItem[], context: FContext) => string | string[]), onChange: (instance: FinderCore<FItem, FContext>, searchTerm: string) => void): SearchEffect<FItem, FContext>;
|
|
412
415
|
declare function transformFilterToSingleValue<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithMultipleValues<FItem, FValue, FContext>;
|
|
413
416
|
declare function transformFilterToBoolean<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithBooleanValue<FItem, boolean, FContext>;
|
|
414
417
|
declare function transformFilterToMultiple<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithMultipleValues<FItem, FValue, FContext>;
|
|
@@ -416,66 +419,15 @@ declare function transformFilterToMultiple<FItem, FValue, FContext = any>(filter
|
|
|
416
419
|
//#region src/react/hooks/use-finder.d.ts
|
|
417
420
|
declare function useFinder<FItem = any, FContext = undefined>(): FinderCore<FItem, FContext>;
|
|
418
421
|
//#endregion
|
|
419
|
-
//#region src/react/components/finder-content-loading.d.ts
|
|
420
|
-
interface FinderContentLoadingProps<FItem, FContext> {
|
|
421
|
-
children: ElementType<FinderContentProps<FItem, FContext>["loading"]> | ReactElement<FinderContentProps<FItem, FContext>["loading"]> | Iterable<ReactNode>;
|
|
422
|
-
}
|
|
423
|
-
declare function FinderContentLoading<FItem, FContext>({
|
|
424
|
-
children: Component
|
|
425
|
-
}: FinderContentLoadingProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
426
|
-
//#endregion
|
|
427
|
-
//#region src/react/components/finder-content-empty.d.ts
|
|
428
|
-
interface FinderContentEmptyProps<FItem, FContext> {
|
|
429
|
-
children: ElementType<FinderContentProps<FItem, FContext>["empty"]> | ReactElement<FinderContentProps<FItem, FContext>["empty"]> | Iterable<ReactNode>;
|
|
430
|
-
}
|
|
431
|
-
declare function FinderContentEmpty<FItem = any, FContext = any>({
|
|
432
|
-
children: Component
|
|
433
|
-
}: FinderContentEmptyProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
434
|
-
//#endregion
|
|
435
|
-
//#region src/react/components/finder-content-no-matches.d.ts
|
|
436
|
-
interface FinderContentNoMatchesProps<FItem, FContext> {
|
|
437
|
-
children: ElementType<FinderContentProps<FItem, FContext>["noMatches"]> | ReactElement<FinderContentProps<FItem, FContext>["noMatches"]> | Iterable<ReactNode>;
|
|
438
|
-
}
|
|
439
|
-
declare function FinderContentNoMatches<FItem = any, FContext = any>({
|
|
440
|
-
children: Component
|
|
441
|
-
}: FinderContentNoMatchesProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
442
|
-
//#endregion
|
|
443
|
-
//#region src/react/components/finder-content-items.d.ts
|
|
444
|
-
interface FinderContentItemsProps<FItem, FContext> {
|
|
445
|
-
children: ElementType<FinderContentProps<FItem, FContext>["items"]> | ReactElement<FinderContentProps<FItem, FContext>["items"]> | Iterable<ReactNode>;
|
|
446
|
-
}
|
|
447
|
-
declare function FinderContentItems<FItem = any, FContext = any>({
|
|
448
|
-
children: Component
|
|
449
|
-
}: FinderContentItemsProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
450
|
-
//#endregion
|
|
451
|
-
//#region src/react/components/finder-content-groups.d.ts
|
|
452
|
-
interface FinderContentGroupsProps<FItem, FContext> {
|
|
453
|
-
children: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
454
|
-
}
|
|
455
|
-
declare function FinderContentGroups<FItem = any, FContext = any>({
|
|
456
|
-
children: Component
|
|
457
|
-
}: FinderContentGroupsProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
458
|
-
//#endregion
|
|
459
422
|
//#region src/react/components/finder-content.d.ts
|
|
460
423
|
interface FinderContentContainerProps<FItem, FContext> {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
groups?: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
declare function FinderContent<FItem = any, FContext = any>({
|
|
470
|
-
children: renderProps
|
|
471
|
-
}: FinderContentContainerProps<FItem, FContext>): (import("react/jsx-runtime").JSX.Element | undefined)[];
|
|
472
|
-
declare namespace FinderContent {
|
|
473
|
-
var Loading: typeof FinderContentLoading;
|
|
474
|
-
var Empty: typeof FinderContentEmpty;
|
|
475
|
-
var NoMatches: typeof FinderContentNoMatches;
|
|
476
|
-
var Items: typeof FinderContentItems;
|
|
477
|
-
var Groups: typeof FinderContentGroups;
|
|
424
|
+
loading?: ElementType<FinderContentProps<FItem, FContext>["loading"]> | ReactElement<FinderContentProps<FItem, FContext>["loading"]> | Iterable<ReactNode>;
|
|
425
|
+
empty?: ElementType<FinderContentProps<FItem, FContext>["empty"]> | ReactElement<FinderContentProps<FItem, FContext>["empty"]> | Iterable<ReactNode>;
|
|
426
|
+
noMatches?: ElementType<FinderContentProps<FItem, FContext>["noMatches"]> | ReactElement<FinderContentProps<FItem, FContext>["noMatches"]> | Iterable<ReactNode>;
|
|
427
|
+
items?: ElementType<FinderContentProps<FItem, FContext>["items"]> | ReactElement<FinderContentProps<FItem, FContext>["items"]> | Iterable<ReactNode>;
|
|
428
|
+
groups?: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
478
429
|
}
|
|
430
|
+
declare function FinderContent<FItem = any, FContext = any>(props: FinderContentContainerProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
479
431
|
//#endregion
|
|
480
432
|
//#region src/react/components/finder-search-term-haystack.d.ts
|
|
481
433
|
interface FinderSearchTermHaystackProps {
|
|
@@ -539,4 +491,4 @@ declare function StringMatch({
|
|
|
539
491
|
Miss
|
|
540
492
|
}: StringMatchProps): string | (string | import("react/jsx-runtime").JSX.Element)[];
|
|
541
493
|
//#endregion
|
|
542
|
-
export { type AnyFilterRuleDefinition, type FilterOption, type FilterRuleDefinition, Finder, type FinderChangeEvent, type FinderConstructorOptions, type FinderContentProps, type FinderEvent, type FinderFirstUserInteractionEvent, type FinderInitEvent, type FinderProps, type FinderReadyEvent, type FinderResultGroup, type GroupByRuleDefinition, type RuleDefinition, type RuleEffect, type
|
|
494
|
+
export { type AnyFilterRuleDefinition, type FilterOption, type FilterRuleDefinition, Finder, type FinderChangeEvent, type FinderConstructorOptions, type FinderContentProps, type FinderEvent, type FinderFirstUserInteractionEvent, type FinderInitEvent, type FinderProps, type FinderReadyEvent, type FinderResultGroup, type GroupByRuleDefinition, type RuleDefinition, type RuleEffect, type SearchRuleDefinition, type SortByRuleDefinition, type SortDirection, StringMatch, type StringMatchSegmentProps, filterRule, finderRuleset, groupByRule, ruleEffect, searchRule, sortByRule, transformFilterToBoolean, transformFilterToMultiple, transformFilterToSingleValue, useFinder, useFinderRef };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,93 +1,5 @@
|
|
|
1
1
|
import { ElementType, PropsWithChildren, ReactElement, ReactNode, RefObject } from "react";
|
|
2
2
|
|
|
3
|
-
//#region src/core/types/rule-types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Select a property from the item to sort by.
|
|
6
|
-
*/
|
|
7
|
-
type FinderPropertySelector<FItem, FContext = any> = (item: FItem, context: FContext) => string | number;
|
|
8
|
-
interface Rule {
|
|
9
|
-
id: string;
|
|
10
|
-
debounceMilliseconds?: number;
|
|
11
|
-
label?: string;
|
|
12
|
-
hidden?: boolean;
|
|
13
|
-
}
|
|
14
|
-
type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, FContext> | FilterRuleDefinition<FItem, FContext> | SortByRuleDefinition<FItem, FContext> | GroupByRuleDefinition<FItem, FContext>;
|
|
15
|
-
interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
|
|
16
|
-
id?: string;
|
|
17
|
-
searchFn?: (item: FItem, context: FContext) => string | string[];
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Describes the display of a filter or sort option.
|
|
21
|
-
*/
|
|
22
|
-
interface FilterOption<FValue = any> {
|
|
23
|
-
label?: string;
|
|
24
|
-
value: FValue;
|
|
25
|
-
disabled?: boolean;
|
|
26
|
-
}
|
|
27
|
-
interface FilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends Rule {
|
|
28
|
-
filterFn: (item: FItem, value: FValue, context: FContext) => boolean;
|
|
29
|
-
required?: boolean;
|
|
30
|
-
strictOptions?: boolean;
|
|
31
|
-
multiple?: boolean;
|
|
32
|
-
boolean?: boolean;
|
|
33
|
-
options?: FilterOption<FValue>[] | ((options: {
|
|
34
|
-
items: FItem[];
|
|
35
|
-
context: FContext;
|
|
36
|
-
}) => FilterOption<FValue>[]);
|
|
37
|
-
}
|
|
38
|
-
type AnyFilterRuleDefinition<FItem = any, FValue = any> = Omit<FilterRuleDefinition<FItem, FValue>, "options">;
|
|
39
|
-
interface FilterRuleWithBooleanValue<FItem = any, FValue = boolean, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
40
|
-
multiple?: false;
|
|
41
|
-
boolean: true;
|
|
42
|
-
defaultValue?: boolean;
|
|
43
|
-
options?: never;
|
|
44
|
-
}
|
|
45
|
-
interface FilterRuleWithSingleValue<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
46
|
-
multiple?: false;
|
|
47
|
-
boolean?: false;
|
|
48
|
-
defaultValue?: FValue;
|
|
49
|
-
}
|
|
50
|
-
interface FilterRuleWithMultipleValues<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
51
|
-
multiple: true;
|
|
52
|
-
boolean?: false;
|
|
53
|
-
defaultValue?: FValue[];
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* A hydrated filter has rendered any option generator functions, and narrowed ambiguous properties from FilterRule.
|
|
57
|
-
*/
|
|
58
|
-
interface HydratedFilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
59
|
-
options?: FilterOption<FValue>[];
|
|
60
|
-
multiple: boolean;
|
|
61
|
-
boolean: boolean;
|
|
62
|
-
_isHydrated: true;
|
|
63
|
-
}
|
|
64
|
-
interface SortByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
65
|
-
sortFn: FinderPropertySelector<FItem, FContext> | FinderPropertySelector<FItem, FContext>[];
|
|
66
|
-
defaultSortDirection?: SortDirection;
|
|
67
|
-
}
|
|
68
|
-
interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
69
|
-
groupFn: (item: FItem, context: FContext) => string | number | (string | number)[];
|
|
70
|
-
sortGroupFn?: FinderPropertySelector<FinderResultGroup<FItem>, FContext>;
|
|
71
|
-
defaultGroupSortDirection?: SortDirection;
|
|
72
|
-
sticky?: {
|
|
73
|
-
header?: string | string[];
|
|
74
|
-
footer?: string | string[];
|
|
75
|
-
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
76
|
-
header?: string | string[];
|
|
77
|
-
footer?: string | string[];
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
interface FilterTestOptions {
|
|
81
|
-
rules: HydratedFilterRuleDefinition[];
|
|
82
|
-
values?: Record<string, any>;
|
|
83
|
-
isAdditive?: boolean;
|
|
84
|
-
}
|
|
85
|
-
interface FilterTestRuleOptions {
|
|
86
|
-
rule: string | AnyFilterRuleDefinition;
|
|
87
|
-
value: any;
|
|
88
|
-
isAdditive?: boolean;
|
|
89
|
-
}
|
|
90
|
-
//#endregion
|
|
91
3
|
//#region src/core/core-constants.d.ts
|
|
92
4
|
declare const EVENTS: {
|
|
93
5
|
readonly INIT: "init";
|
|
@@ -189,6 +101,7 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
189
101
|
get search(): {
|
|
190
102
|
rule: SearchRuleDefinition<unknown, any> | undefined;
|
|
191
103
|
searchTerm: string;
|
|
104
|
+
suggestedFilters: SearchRuleSuggestion[];
|
|
192
105
|
hasSearchTerm: boolean;
|
|
193
106
|
hasSearchRule: boolean;
|
|
194
107
|
setSearchTerm: (value: string) => void;
|
|
@@ -197,8 +110,11 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
197
110
|
};
|
|
198
111
|
get filters(): {
|
|
199
112
|
values: Record<string, any>;
|
|
200
|
-
raw:
|
|
113
|
+
raw: {
|
|
114
|
+
[k: string]: any;
|
|
115
|
+
};
|
|
201
116
|
activeRules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
117
|
+
touchedRules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
202
118
|
rules: HydratedFilterRuleDefinition<any, any, any>[];
|
|
203
119
|
isActive: (identifier: string | AnyFilterRuleDefinition<any, any>) => boolean;
|
|
204
120
|
get: (identifier: string | AnyFilterRuleDefinition<any, any>) => any;
|
|
@@ -247,7 +163,7 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
247
163
|
numTotalItems: number;
|
|
248
164
|
lastPage: number | undefined;
|
|
249
165
|
isPaginated: boolean;
|
|
250
|
-
setPage: (value
|
|
166
|
+
setPage: (value?: number) => void;
|
|
251
167
|
setNumItemsPerPage: (value?: number) => void;
|
|
252
168
|
};
|
|
253
169
|
/**
|
|
@@ -256,8 +172,8 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
256
172
|
setItems(items: FItem[] | null | undefined): void;
|
|
257
173
|
setIsLoading(value?: boolean): void;
|
|
258
174
|
setIsDisabled(value?: boolean): void;
|
|
259
|
-
setRules(definitions
|
|
260
|
-
setContext(context
|
|
175
|
+
setRules(definitions?: RuleDefinition<FItem>[]): void;
|
|
176
|
+
setContext(context?: FContext): void;
|
|
261
177
|
/**
|
|
262
178
|
* Utils
|
|
263
179
|
*/
|
|
@@ -266,20 +182,104 @@ declare class FinderCore<FItem = any, FContext = any> {
|
|
|
266
182
|
toJSON(): Omit<FinderConstructorOptions<FItem, any>, "rules">;
|
|
267
183
|
}
|
|
268
184
|
//#endregion
|
|
269
|
-
//#region src/core/types/
|
|
185
|
+
//#region src/core/types/rule-types.d.ts
|
|
186
|
+
/**
|
|
187
|
+
* Select a property from the item to sort by.
|
|
188
|
+
*/
|
|
189
|
+
type FinderPropertySelector<FItem, FContext = any> = (item: FItem, context: FContext) => string | number;
|
|
190
|
+
interface Rule {
|
|
191
|
+
id: string;
|
|
192
|
+
debounceMilliseconds?: number;
|
|
193
|
+
label?: string;
|
|
194
|
+
hidden?: boolean;
|
|
195
|
+
}
|
|
196
|
+
type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, FContext> | FilterRuleDefinition<FItem, FContext> | SortByRuleDefinition<FItem, FContext> | GroupByRuleDefinition<FItem, FContext>;
|
|
197
|
+
interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
|
|
198
|
+
id?: string;
|
|
199
|
+
searchFn?: (item: FItem, context: FContext) => string | string[];
|
|
200
|
+
suggestFiltersFrom?: string | AnyFilterRuleDefinition | (string | AnyFilterRuleDefinition)[];
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Describes the display of a filter or sort option.
|
|
204
|
+
*/
|
|
205
|
+
interface FilterOption<FValue = any> {
|
|
206
|
+
label?: string;
|
|
207
|
+
value: FValue;
|
|
208
|
+
disabled?: boolean;
|
|
209
|
+
}
|
|
210
|
+
interface FilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends Rule {
|
|
211
|
+
filterFn: (item: FItem, value: FValue, context: FContext) => boolean;
|
|
212
|
+
required?: boolean;
|
|
213
|
+
strictOptions?: boolean;
|
|
214
|
+
multiple?: boolean;
|
|
215
|
+
boolean?: boolean;
|
|
216
|
+
options?: FilterOption<FValue>[] | ((options: {
|
|
217
|
+
items: FItem[];
|
|
218
|
+
context: FContext;
|
|
219
|
+
}) => FilterOption<FValue>[]);
|
|
220
|
+
defaultValue?: any;
|
|
221
|
+
}
|
|
222
|
+
type AnyFilterRuleDefinition<FItem = any, FValue = any> = Omit<FilterRuleDefinition<FItem, FValue>, "options">;
|
|
223
|
+
interface FilterRuleWithBooleanValue<FItem = any, FValue = boolean, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
224
|
+
multiple?: false;
|
|
225
|
+
boolean: true;
|
|
226
|
+
defaultValue?: boolean;
|
|
227
|
+
options?: never;
|
|
228
|
+
}
|
|
229
|
+
interface FilterRuleWithSingleValue<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
230
|
+
multiple?: false;
|
|
231
|
+
boolean?: false;
|
|
232
|
+
defaultValue?: FValue;
|
|
233
|
+
}
|
|
234
|
+
interface FilterRuleWithMultipleValues<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
235
|
+
multiple: true;
|
|
236
|
+
boolean?: false;
|
|
237
|
+
defaultValue?: FValue[];
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* A hydrated filter has rendered any option generator functions, and narrowed ambiguous properties from FilterRule.
|
|
241
|
+
*/
|
|
242
|
+
interface HydratedFilterRuleDefinition<FItem = any, FValue = any, FContext = any> extends FilterRuleDefinition<FItem, FValue, FContext> {
|
|
243
|
+
options?: FilterOption<FValue>[];
|
|
244
|
+
multiple: boolean;
|
|
245
|
+
boolean: boolean;
|
|
246
|
+
_isHydrated: true;
|
|
247
|
+
}
|
|
248
|
+
interface SortByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
249
|
+
sortFn: FinderPropertySelector<FItem, FContext> | FinderPropertySelector<FItem, FContext>[];
|
|
250
|
+
defaultSortDirection?: SortDirection;
|
|
251
|
+
}
|
|
252
|
+
interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
253
|
+
groupFn: (item: FItem, context: FContext) => string | number | (string | number)[];
|
|
254
|
+
sortGroupFn?: FinderPropertySelector<FinderResultGroup<FItem>, FContext>;
|
|
255
|
+
defaultGroupSortDirection?: SortDirection;
|
|
256
|
+
sticky?: {
|
|
257
|
+
header?: string | string[];
|
|
258
|
+
footer?: string | string[];
|
|
259
|
+
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
260
|
+
header?: string | string[];
|
|
261
|
+
footer?: string | string[];
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
interface FilterTestOptions {
|
|
265
|
+
rules: HydratedFilterRuleDefinition[];
|
|
266
|
+
values?: Record<string, any>;
|
|
267
|
+
isAdditive?: boolean;
|
|
268
|
+
}
|
|
269
|
+
interface FilterTestRuleOptions {
|
|
270
|
+
rule: string | AnyFilterRuleDefinition;
|
|
271
|
+
value: any;
|
|
272
|
+
isAdditive?: boolean;
|
|
273
|
+
}
|
|
270
274
|
interface RuleEffect<FItem = any, FContext = any> {
|
|
271
275
|
rules: string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[] | ((items: FItem[], context: FContext) => string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[]);
|
|
272
276
|
onChange: (instance: FinderCore<FItem, FContext>, rule: RuleDefinition<FItem>) => void;
|
|
273
277
|
}
|
|
274
|
-
interface SearchEffect<FItem = any, FContext = any> {
|
|
275
|
-
haystack: string | string[] | ((items: FItem[], context: FContext) => string | string[]);
|
|
276
|
-
onChange: (instance: FinderCore<FItem, FContext>, searchTerm: string) => void;
|
|
277
|
-
}
|
|
278
278
|
//#endregion
|
|
279
279
|
//#region src/core/types/core-types.d.ts
|
|
280
280
|
interface FinderConstructorOptions<FItem, FContext = any> {
|
|
281
|
-
rules
|
|
282
|
-
effects?:
|
|
281
|
+
rules?: RuleDefinition<FItem>[];
|
|
282
|
+
effects?: RuleEffect[];
|
|
283
283
|
context?: FContext;
|
|
284
284
|
isLoading?: boolean;
|
|
285
285
|
disabled?: boolean;
|
|
@@ -349,6 +349,10 @@ interface PaginationMixinInterface {
|
|
|
349
349
|
setPage: (value: number) => void;
|
|
350
350
|
setNumItemsPerPage: (value: number) => void;
|
|
351
351
|
}
|
|
352
|
+
interface SearchRuleSuggestion {
|
|
353
|
+
rule: HydratedFilterRuleDefinition;
|
|
354
|
+
optionMatches?: FilterOption[];
|
|
355
|
+
}
|
|
352
356
|
//#endregion
|
|
353
357
|
//#region src/core/types/string-match-types.d.ts
|
|
354
358
|
/**
|
|
@@ -396,7 +400,7 @@ interface StringMatchSegmentProps {
|
|
|
396
400
|
}
|
|
397
401
|
type FinderSearchTermProp = keyof HTMLElementTagNameMap | ElementType | ElementType<StringMatchSegmentProps>;
|
|
398
402
|
//#endregion
|
|
399
|
-
//#region src/core/utils/rule-type-
|
|
403
|
+
//#region src/core/utils/rule-type-guards.d.ts
|
|
400
404
|
/**
|
|
401
405
|
* Enforce structure for an array of rule of mixed types.
|
|
402
406
|
*/
|
|
@@ -408,7 +412,6 @@ declare function filterRule<FItem, FValue = any, FContext = any, T = FilterRuleW
|
|
|
408
412
|
declare function sortByRule<FItem, FContext = any>(rule: SortByRuleDefinition<FItem, FContext>): SortByRuleDefinition<FItem, FContext>;
|
|
409
413
|
declare function groupByRule<FItem, FContext = any>(rule: GroupByRuleDefinition<FItem, FContext>): GroupByRuleDefinition<FItem, FContext>;
|
|
410
414
|
declare function ruleEffect<FItem, FContext = any>(rules: string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[] | ((items: FItem[], context: FContext) => string | RuleDefinition<FItem> | (string | RuleDefinition<FItem>)[]), onChange: (instance: FinderCore<FItem, FContext>, rule: RuleDefinition) => void): RuleEffect<FItem, FContext>;
|
|
411
|
-
declare function searchEffect<FItem, FContext = any>(haystack: string | string[] | ((items: FItem[], context: FContext) => string | string[]), onChange: (instance: FinderCore<FItem, FContext>, searchTerm: string) => void): SearchEffect<FItem, FContext>;
|
|
412
415
|
declare function transformFilterToSingleValue<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithMultipleValues<FItem, FValue, FContext>;
|
|
413
416
|
declare function transformFilterToBoolean<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithBooleanValue<FItem, boolean, FContext>;
|
|
414
417
|
declare function transformFilterToMultiple<FItem, FValue, FContext = any>(filter: AnyFilterRuleDefinition<FItem, FValue>): FilterRuleWithMultipleValues<FItem, FValue, FContext>;
|
|
@@ -416,66 +419,15 @@ declare function transformFilterToMultiple<FItem, FValue, FContext = any>(filter
|
|
|
416
419
|
//#region src/react/hooks/use-finder.d.ts
|
|
417
420
|
declare function useFinder<FItem = any, FContext = undefined>(): FinderCore<FItem, FContext>;
|
|
418
421
|
//#endregion
|
|
419
|
-
//#region src/react/components/finder-content-loading.d.ts
|
|
420
|
-
interface FinderContentLoadingProps<FItem, FContext> {
|
|
421
|
-
children: ElementType<FinderContentProps<FItem, FContext>["loading"]> | ReactElement<FinderContentProps<FItem, FContext>["loading"]> | Iterable<ReactNode>;
|
|
422
|
-
}
|
|
423
|
-
declare function FinderContentLoading<FItem, FContext>({
|
|
424
|
-
children: Component
|
|
425
|
-
}: FinderContentLoadingProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
426
|
-
//#endregion
|
|
427
|
-
//#region src/react/components/finder-content-empty.d.ts
|
|
428
|
-
interface FinderContentEmptyProps<FItem, FContext> {
|
|
429
|
-
children: ElementType<FinderContentProps<FItem, FContext>["empty"]> | ReactElement<FinderContentProps<FItem, FContext>["empty"]> | Iterable<ReactNode>;
|
|
430
|
-
}
|
|
431
|
-
declare function FinderContentEmpty<FItem = any, FContext = any>({
|
|
432
|
-
children: Component
|
|
433
|
-
}: FinderContentEmptyProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
434
|
-
//#endregion
|
|
435
|
-
//#region src/react/components/finder-content-no-matches.d.ts
|
|
436
|
-
interface FinderContentNoMatchesProps<FItem, FContext> {
|
|
437
|
-
children: ElementType<FinderContentProps<FItem, FContext>["noMatches"]> | ReactElement<FinderContentProps<FItem, FContext>["noMatches"]> | Iterable<ReactNode>;
|
|
438
|
-
}
|
|
439
|
-
declare function FinderContentNoMatches<FItem = any, FContext = any>({
|
|
440
|
-
children: Component
|
|
441
|
-
}: FinderContentNoMatchesProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
442
|
-
//#endregion
|
|
443
|
-
//#region src/react/components/finder-content-items.d.ts
|
|
444
|
-
interface FinderContentItemsProps<FItem, FContext> {
|
|
445
|
-
children: ElementType<FinderContentProps<FItem, FContext>["items"]> | ReactElement<FinderContentProps<FItem, FContext>["items"]> | Iterable<ReactNode>;
|
|
446
|
-
}
|
|
447
|
-
declare function FinderContentItems<FItem = any, FContext = any>({
|
|
448
|
-
children: Component
|
|
449
|
-
}: FinderContentItemsProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
450
|
-
//#endregion
|
|
451
|
-
//#region src/react/components/finder-content-groups.d.ts
|
|
452
|
-
interface FinderContentGroupsProps<FItem, FContext> {
|
|
453
|
-
children: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
454
|
-
}
|
|
455
|
-
declare function FinderContentGroups<FItem = any, FContext = any>({
|
|
456
|
-
children: Component
|
|
457
|
-
}: FinderContentGroupsProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
458
|
-
//#endregion
|
|
459
422
|
//#region src/react/components/finder-content.d.ts
|
|
460
423
|
interface FinderContentContainerProps<FItem, FContext> {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
groups?: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
declare function FinderContent<FItem = any, FContext = any>({
|
|
470
|
-
children: renderProps
|
|
471
|
-
}: FinderContentContainerProps<FItem, FContext>): (import("react/jsx-runtime").JSX.Element | undefined)[];
|
|
472
|
-
declare namespace FinderContent {
|
|
473
|
-
var Loading: typeof FinderContentLoading;
|
|
474
|
-
var Empty: typeof FinderContentEmpty;
|
|
475
|
-
var NoMatches: typeof FinderContentNoMatches;
|
|
476
|
-
var Items: typeof FinderContentItems;
|
|
477
|
-
var Groups: typeof FinderContentGroups;
|
|
424
|
+
loading?: ElementType<FinderContentProps<FItem, FContext>["loading"]> | ReactElement<FinderContentProps<FItem, FContext>["loading"]> | Iterable<ReactNode>;
|
|
425
|
+
empty?: ElementType<FinderContentProps<FItem, FContext>["empty"]> | ReactElement<FinderContentProps<FItem, FContext>["empty"]> | Iterable<ReactNode>;
|
|
426
|
+
noMatches?: ElementType<FinderContentProps<FItem, FContext>["noMatches"]> | ReactElement<FinderContentProps<FItem, FContext>["noMatches"]> | Iterable<ReactNode>;
|
|
427
|
+
items?: ElementType<FinderContentProps<FItem, FContext>["items"]> | ReactElement<FinderContentProps<FItem, FContext>["items"]> | Iterable<ReactNode>;
|
|
428
|
+
groups?: ElementType<FinderContentProps<FItem, FContext>["groups"]> | ReactElement<FinderContentProps<FItem, FContext>["groups"]> | Iterable<ReactNode>;
|
|
478
429
|
}
|
|
430
|
+
declare function FinderContent<FItem = any, FContext = any>(props: FinderContentContainerProps<FItem, FContext>): import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null;
|
|
479
431
|
//#endregion
|
|
480
432
|
//#region src/react/components/finder-search-term-haystack.d.ts
|
|
481
433
|
interface FinderSearchTermHaystackProps {
|
|
@@ -539,4 +491,4 @@ declare function StringMatch({
|
|
|
539
491
|
Miss
|
|
540
492
|
}: StringMatchProps): string | (string | import("react/jsx-runtime").JSX.Element)[];
|
|
541
493
|
//#endregion
|
|
542
|
-
export { type AnyFilterRuleDefinition, type FilterOption, type FilterRuleDefinition, Finder, type FinderChangeEvent, type FinderConstructorOptions, type FinderContentProps, type FinderEvent, type FinderFirstUserInteractionEvent, type FinderInitEvent, type FinderProps, type FinderReadyEvent, type FinderResultGroup, type GroupByRuleDefinition, type RuleDefinition, type RuleEffect, type
|
|
494
|
+
export { type AnyFilterRuleDefinition, type FilterOption, type FilterRuleDefinition, Finder, type FinderChangeEvent, type FinderConstructorOptions, type FinderContentProps, type FinderEvent, type FinderFirstUserInteractionEvent, type FinderInitEvent, type FinderProps, type FinderReadyEvent, type FinderResultGroup, type GroupByRuleDefinition, type RuleDefinition, type RuleEffect, type SearchRuleDefinition, type SortByRuleDefinition, type SortDirection, StringMatch, type StringMatchSegmentProps, filterRule, finderRuleset, groupByRule, ruleEffect, searchRule, sortByRule, transformFilterToBoolean, transformFilterToMultiple, transformFilterToSingleValue, useFinder, useFinderRef };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createContext as e,useContext as t,useImperativeHandle as n,useMemo as r,useRef as i,useState as a}from"react";import{jsx as o}from"react/jsx-runtime";import{clamp as s,debounce as c,isEqual as l,orderBy as u,range as d,uniqBy as f}from"lodash";function p(e){return e}function m(e){return e}function h(e){return e}function g(e){return e}function _(e){return e}function v(e,t){return{rules:e,onChange:t}}function y(e,t){return{haystack:e,onChange:t}}function b(e){let t={...e};return delete t.boolean,delete t.multiple,t}function x(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function S(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const C=e(null);function w(){let e=t(C);if(e===null)throw Error(`useFinder requires a FinderContext.`);let[n]=e;return n}function T({children:e}){let t=w();return t.state===`loading`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function E({children:e}){let t=w();return t.state===`empty`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function D({children:e}){let t=w();return t.state===`noMatches`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function O({children:e}){let t=w();return t.state===`items`&&t.matches.items?typeof e==`function`?o(e,{items:t.matches.items,pagination:t.pagination,context:t.context}):e:null}function k({children:e}){let t=w();return t.state===`groups`&&t.matches.groups?typeof e==`function`?o(e,{groups:t.matches.groups,pagination:t.pagination,context:t.context}):e:null}function A({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&o(T,{children:e.loading},`loading`),e.empty&&o(E,{children:e.empty},`empty`),e.noMatches&&o(D,{children:e.noMatches},`noMatches`),e.items&&o(O,{children:e.items},`items`),e.groups&&o(k,{children:e.groups},`groups`)]}A.Loading=T,A.Empty=E,A.NoMatches=D,A.Items=O,A.Groups=k;function j(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function M(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=ee(e,j(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=te(e,j(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function ee(e,t){if(e.length<t.length)return;let n=e.indexOf(t);if(n!==-1)return d(n,n+t.length).map(e=>e)}function te(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var ne=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function re(e,t){return(Array.isArray(e)?e:[e]).map(e=>new ne(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=M(n.transformed,t);return r===void 0?e:oe(ae(n,ie(r,n.transformed)))},void 0)}function ie(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function ae(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function oe(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function se(e,t){return(Array.isArray(e)?e:[e]).some(e=>M(j(e),t)!==void 0)}function N({needle:e,haystack:t,Match:n=`mark`,Miss:i}){let a=r(()=>re(t,e),[t,e]);return a===void 0?t:a.map((e,t)=>{let r=[e.value,t].join();return e.is_match?typeof n==`string`?o(n,{"data-is-match":e.is_match,children:e.value},r):o(n,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r):i===void 0?e.value:typeof i==`string`?o(i,{"data-is-match":e.is_match,children:e.value},r):o(i,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r)})}function P({Match:e=`mark`,Miss:t,children:n}){let r=w();return r.search.hasSearchTerm===!1?n:o(N,{needle:r.search.searchTerm,haystack:n,Match:e,Miss:t})}function ce(){let e=new Map;return(t,n)=>t.debounceMilliseconds===void 0?n():(e.has(t)===!1&&e.set(t,c(e=>e(),t.debounceMilliseconds)),e.get(t)?.(n))}var le=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const F={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},I={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},L={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var R=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function z(e){return typeof e==`object`&&!!e&&`sortFn`in e}function B(e){return typeof e==`object`&&!!e&&`searchFn`in e}function V(e){return typeof e==`object`&&!!e&&`filterFn`in e}function H(e){return V(e)&&typeof e.options!=`function`}function U(e){return typeof e==`object`&&!!e&&`groupFn`in e}function ue(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function de(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function fe(e){return V(e)&&e.boolean===!0}function pe(e){return V(e)&&e.multiple===!0}function me(e){return V(e)&&e.multiple!==!0&&e.boolean!==!0}function he(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new R(L.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new R(L.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new R(L.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new R(L.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function ge(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new R(L.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new R(L.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new R(L.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new R(L.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new R(L.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function _e(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new R(L.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new R(L.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new R(L.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function W(e){if(pe(e))return ge(e);if(fe(e))return he(e);if(me(e))return _e(e);throw new R(L.INVALID_RULE_SHAPE,{definition:e})}var G=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&W(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:I.FILTERS,event:F.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(H)}get activeRules(){return this.rules.filter(e=>W(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return W(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!H(t))throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new R(L.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=W(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:I.FILTERS,event:F.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=f([...this.rules,...e.rules],`id`),n={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:n}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new R(L.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>H(t)&&W(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>H(r)&&W(r).isMatch(t,e.values[r.id],n)))}},K=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(U(t)===!1)throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(U)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:I.GROUP_BY,event:F.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:I.GROUP_BY,event:F.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,n){let r=t.reduce((t,r)=>{let i=e.rule?.groupFn(r,n);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(r)}),t},{}),i=Object.entries(r).map(([e,t])=>({id:e,items:t})),a=e.rule?.sticky!==void 0,o=[],s=[];if(a&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(i,n):e.rule.sticky;t&&(o.push(ve(t)),s.push(`asc`))}return e.rule?.sortGroupFn&&(o.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,n)),s.push(e.groupBySortDirection??`asc`)),o.length>0?u(i,o,s):i}};function ve({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var q=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:I.PAGINATION,event:F.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:I.PAGINATION,event:F.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?s(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?(s(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let n=Math.ceil(t.length/e.numItemsPerPage),r=(s(e.page,1,n)-1)*e.numItemsPerPage;return t.slice(r,r+e.numItemsPerPage)}};function ye(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function be(e,t,n){let r=t.reduce((t,r)=>{if(e.rule?.searchFn===void 0)return t;let i=e.rule.searchFn(r,n),a=(Array.isArray(i)?i.map(j):[j(i)]).reduce((t,n)=>{let r=M(n,e.searchTerm);return r!==void 0&&t.push(ye(r,n)),t},[]);if(a.length>0){let e=u(a,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:r,score:e})}return t},[]),i=r.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return u(r,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/i*100],[`desc`]).map(e=>e.item)}var J=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(B)===void 0)throw new R(L.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(B)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}setSearchTerm(e){let t=this.rule;if(!t)throw new R(L.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new R(L.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:I.SEARCH,event:F.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:I.SEARCH,event:F.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new R(L.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:be(e,t,n)}};const Y=[void 0,`desc`,`asc`];var X=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(z(t)===!1)throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(z)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:I.SORT_BY,event:F.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=Y.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(Y.length-1);this.setSortDirection(Y[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:I.SORT_BY,event:F.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,n){return e.rule===void 0?t:u(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,n)),e.sortDirection)}},Z=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?q.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=K.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=J.process(e.search,r,n)),e.filters&&(r=G.process(e.filters,r,n)),e.sortBy&&(r=X.process(e.sortBy,r,n)),r}},xe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},Se=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(V(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new R(L.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[B,V,z,U],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!B(e))throw new R(L.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new R(L.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new R(L.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},Ce=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{se(n.haystack,e)&&n.onChange(t,e)})}},we=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new Se(e),this.ruleEffects=new xe(t),this.searchEffects=new Ce(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){B(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},Q=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new le,this.#i=new Z,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(ue)??[],C=n?.filter(de)??[];this.#a=new we({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:ce()};this.search=new J({initialSearchTerm:r},w),this.filters=new G({initialFilters:c},w),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new q({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:I.CORE,event:F.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(F.CHANGE,b),y&&this.#r.on(F.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:I.CORE,event:F.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(F.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(F.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(F.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:I.CORE,event:F.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(F.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(F.READY,{source:I.CORE,event:F.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Z.test({mixins:t,items:this.items,context:this.context})}return Z.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if(l(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:I.CORE,event:F.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:I.CORE,event:F.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:I.CORE,event:F.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){l(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;l(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:I.CORE,event:F.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},Te=class{#e;constructor(e,t){let n=()=>this;this.#e=new Q(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:e,rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w,controllerRef:T,children:E}){let[D,O]=a(Date.now()),[k]=a(()=>{let n=new Te(e,{rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w});return n.events.on(F.SYNC_UPDATED_AT,e=>O(e)),n});k.setItems(e),k.setIsLoading(m),k.setIsDisabled(h),k.setRules(t),p!==void 0&&k.setContext(p),g!==void 0&&k.pagination.setPage(g),_!==void 0&&k.pagination.setNumItemsPerPage(_),n(T,()=>k,[k]);let A=r(()=>[k,D],[k,D]);return o(C.Provider,{value:A,children:E})}$.Content=A,$.SearchTermHaystack=P;function Ee(){return i(null)}export{$ as Finder,N as StringMatch,h as filterRule,p as finderRuleset,_ as groupByRule,v as ruleEffect,y as searchEffect,m as searchRule,g as sortByRule,x as transformFilterToBoolean,S as transformFilterToMultiple,b as transformFilterToSingleValue,w as useFinder,Ee as useFinderRef};
|
|
1
|
+
import{createContext as e,useContext as t,useImperativeHandle as n,useMemo as r,useRef as i,useState as a}from"react";import{jsx as o}from"react/jsx-runtime";import{clamp as s,debounce as c,isEqual as l,orderBy as u,partition as d,range as f,uniqBy as p}from"lodash";function m(e){return e}function h(e){return e}function g(e){return e}function _(e){return e}function v(e){return e}function y(e,t){return{rules:e,onChange:t}}function b(e){let t={...e};return delete t.boolean,delete t.multiple,t}function x(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function S(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const C=e(null);function w(){let e=t(C);if(e===null)throw Error(`useFinder requires a FinderContext.`);let[n]=e;return n}function T(e){let t=w();if(Object.values(e).length===0)throw Error(`No render props were found.`);let{loading:n,empty:r,noMatches:i,items:a,groups:s}=e;return t.state===`loading`?typeof n==`function`?o(n,{pagination:t.pagination,context:t.context}):n??null:t.state===`empty`?typeof r==`function`?o(r,{pagination:t.pagination,context:t.context}):r??null:t.state===`noMatches`?typeof i==`function`?o(i,{pagination:t.pagination,context:t.context}):i??null:t.state===`items`&&t.matches.items?typeof a==`function`?o(a,{items:t.matches.items,pagination:t.pagination,context:t.context}):a??null:t.state===`groups`&&t.matches.groups?typeof s==`function`?o(s,{groups:t.matches.groups,pagination:t.pagination,context:t.context}):s??null:null}function E(e,t,n){let r=new RegExp(/"(.*?)"/g),i=t,a=[],o,s=!1;for(;(o=r.exec(t))!==null&&s===!1;){let t=n(String(o[1])),r=D(e.transformed,t);r===void 0?s=!0:(a=a.concat(r),i=i.replace(o[0],``))}if(s)return;let c=n(i),l=O(e.transformed,c);if(l!==void 0)return a=a.concat(l),a.sort((e,t)=>e-t)}function D(e,t){if(e.length<t.length)return;let n=e.indexOf(t);if(n!==-1)return f(n,n+t.length).map(e=>e)}function O(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}function k(e,t,n){let r=E(e,t,n);if(r!==void 0)return te(ee(e,A(r,e.transformed)))}function A(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function ee(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.raw.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.raw.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.raw.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.raw.length){let t=a+s.length,r=e.raw.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function te(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}var ne=class e{#e;constructor(t){this.raw=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}},j=class{#e;#t;#n;constructor(e,t){this.needle=t,this.haystack=new ne(e)}get characterIndexes(){return this.#e===void 0&&(this.#e=E(this.haystack,this.needle,this.#r)),this.#e}get hasMatch(){return this.characterIndexes!==void 0}get score(){if(this.#t===void 0&&this.characterIndexes!==void 0){let e=this.characterIndexes.length/this.haystack.raw.length,t=1,n=1,r=0;for(let e=0;e<this.characterIndexes.length;e+=1)r!==void 0&&this.characterIndexes.at(e)===r+1&&(n+=1,n>=t&&(t=n)),r=this.characterIndexes.at(e);this.#t={percentOfHaystackMatched:e,longestSequentialSequence:t}}return this.#t}get segments(){return this.#n===void 0&&(this.#n=k(this.haystack,this.needle,this.#r)),this.#n}#r(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}};function M({needle:e,haystack:t,Match:n=`mark`,Miss:i}){let a=r(()=>new j(t,e),[t,e]);return a.hasMatch===!1||a.segments===void 0?t:a.segments.map((e,t)=>{let r=[e.value,t].join();return e.is_match?typeof n==`string`?o(n,{"data-is-match":e.is_match,children:e.value},r):o(n,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r):i===void 0?e.value:typeof i==`string`?o(i,{"data-is-match":e.is_match,children:e.value},r):o(i,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r)})}function re({Match:e=`mark`,Miss:t,children:n}){let r=w();return r.search.hasSearchTerm===!1?n:o(M,{needle:r.search.searchTerm,haystack:n,Match:e,Miss:t})}function ie(){let e=new Map;return(t,n)=>t.debounceMilliseconds===void 0?n():(e.has(t)===!1&&e.set(t,c(e=>e(),t.debounceMilliseconds)),e.get(t)?.(n))}var ae=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}get isSilenced(){return this.#t}};const N={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},P={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},F={INITIAL_RULE_NOT_FOUND:`Finder ignored an initial rule that could not be found.`,INITIAL_SEARCH_RULE_NOT_FOUND:`Finder ignored an initialSearchTerm, as no SearchRule was found.`,GROUP_SORT_DIRECTION_SET_WITHOUT_SORT_FN:`The active groupBy rule has a defined sort direction, but no corresponding sortGroupFn.`},I={INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`,RULE_NOT_FOUND:`Finder could not find requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search term must be a string.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`};var L=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function R(e){return typeof e==`object`&&!!e&&`sortFn`in e}function z(e){return typeof e==`object`&&!!e&&`searchFn`in e}function B(e){return typeof e==`object`&&!!e&&`filterFn`in e}function V(e){return B(e)&&typeof e.options!=`function`}function H(e){return typeof e==`object`&&!!e&&`groupFn`in e}function oe(e){return B(e)&&e.boolean===!0}function se(e){return B(e)&&e.multiple===!0}function ce(e){return B(e)&&e.multiple!==!0&&e.boolean!==!0}function U(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new L(I.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new L(I.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new L(I.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new L(I.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function le(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new L(I.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new L(I.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new L(I.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new L(I.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new L(I.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function ue(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new L(I.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(t,n){if(n===void 0)return t!==void 0;let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return r!==void 0&&t===r.value},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new L(I.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new L(I.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function W(e){if(se(e))return le(e);if(oe(e))return U(e);if(ce(e))return ue(e);throw new L(I.INVALID_RULE_SHAPE,{definition:e})}var G=class{#e=new Map;#t=new Map;#n;constructor({initialFilters:e},t){this.#n=t,e&&Object.entries(e).forEach(([e,t])=>{this.#n.getRuleBook().hasRule(e)?(this.#e.set(e,t),this.#t.set(e,t)):console.warn(F.INITIAL_RULE_NOT_FOUND,{rule:e})})}set(e,t){if(this.#n.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&W(n).validate(a),!(this.#e.has(n.id)&&this.#e.get(n.id)===a)&&this.#n.debouncer(n,()=>{let e=a===this.#t.has(n.id),t=a===n.defaultValue;e||t?this.#e.delete(n.id):this.#e.set(n.id,a),this.#n.touch({source:P.FILTERS,event:N.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#n.getRuleBook().rules.filter(V)}get activeRules(){return this.rules.filter(e=>W(e).isActive(this.#e.get(e.id)))}get touchedRules(){return this.rules.filter(e=>this.#e.has(e.id))}get(e){let t=this.getRule(e),n=this.#e.get(t.id);return W(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e.get(n.id);return W(n).has(r,t)}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(!V(t))throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e.get(n.id);this.set(n,W(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e.get(n.id);this.set(n,W(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e.get(n.id);if(n.boolean&&t!==void 0)throw new L(I.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=W(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e.clear(),this.#n.touch({source:P.FILTERS,event:N.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e.get(t.id);return W(t).isActive(n)}test(e){if(this.#n.isLoading())return[];if(e.isAdditive){let t=p([...this.rules,...e.rules],`id`),n={...this.values,...e.values};return this.#n.test({filters:{rules:t,values:n}},!0)}return this.#n.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#n.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new L(I.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return Object.fromEntries(this.#e.entries())}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,touchedRules:this.touchedRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>V(t)&&W(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>V(r)&&W(r).isMatch(t,e.values[r.id],n)))}},K=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#n.getRuleBook().hasRule(e)?this.#e=this.getRule(e):console.warn(F.INITIAL_RULE_NOT_FOUND,{initialGroupBy:e})),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(H(t)===!1)throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(H)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,n){let r=t.reduce((t,r)=>{let i=e.rule?.groupFn(r,n);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(r)}),t},{}),i=Object.entries(r).map(([e,t])=>({id:e,items:t})),a=e.rule?.sticky!==void 0,o=[],s=[];if(a&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(i,n):e.rule.sticky;t&&(o.push(de(t)),s.push(`asc`))}return e.rule?.sortGroupFn&&(o.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,n)),s.push(e.groupBySortDirection??`asc`)),e.rule?.sortGroupFn===void 0&&e.groupBySortDirection!==void 0&&console.warn(F.GROUP_SORT_DIRECTION_SET_WITHOUT_SORT_FN,{rule:e.rule}),o.length>0?u(i,o,s):i}};function de({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var q=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==void 0&&e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:P.PAGINATION,event:N.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:P.PAGINATION,event:N.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?s(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?(s(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let n=Math.ceil(t.length/e.numItemsPerPage),r=(s(e.page,1,n)-1)*e.numItemsPerPage;return t.slice(r,r+e.numItemsPerPage)}};function J(e,t,n){let r=t.reduce((t,r)=>{if(e.rule?.searchFn===void 0)return t;let i=e.rule.searchFn(r,n),a=(Array.isArray(i)?i:[i]).map(t=>new j(t,e.searchTerm)).filter(e=>e.hasMatch).map(e=>e.score);if(a.length>0){let e=u(a,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:r,score:e})}return t},[]),i=r.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return u(r,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/i*100],[`desc`]).map(e=>e.item)}var Y=class{#e;constructor({initialSearchTerm:e},t){this.searchTerm=``,e&&t.getRuleBook().rules.find(z)===void 0&&console.warn(F.INITIAL_SEARCH_RULE_NOT_FOUND,{initialSearchTerm:e}),this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(z)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}get suggestedFilters(){let e=[],t=this.rule;if(this.hasSearchTerm&&t?.suggestFiltersFrom){let n=this.#e.getRuleBook().rules.filter(V),[r,i]=d((Array.isArray(t.suggestFiltersFrom)?t.suggestFiltersFrom:[t.suggestFiltersFrom]).map(e=>{let t=n.find(t=>typeof e==`string`?t.id===e:e.id===t.id);if(t===void 0)throw new L(I.RULE_NOT_FOUND,{rule:t});return t}),e=>e.options!==void 0&&e.options.length>0);r.forEach(t=>{if(t.options&&t.options.length>0){let n=J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},t.options??[]);n.length>0&&e.push({rule:t,optionMatches:n})}}),i.length>0&&J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},i).forEach(t=>{e.push({rule:t})})}return e}setSearchTerm(e){let t=this.rule;if(!t)throw new L(I.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new L(I.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:P.SEARCH,event:N.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:P.SEARCH,event:N.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,suggestedFilters:this.suggestedFilters,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new L(I.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:J(e,t,n)}};const X=[void 0,`desc`,`asc`];var Z=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#n.getRuleBook().hasRule(e)?this.#e=this.getRule(e):console.warn(F.INITIAL_RULE_NOT_FOUND,{initialSortBy:e})),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(R(t)===!1)throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(R)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=X.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(X.length-1);this.setSortDirection(X[t])}}toggleSortDirection(){if(this.sortDirection===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,n){return e.rule===void 0?t:u(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,n)),e.sortDirection)}},Q=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?q.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=K.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=Y.process(e.search,r,n)),e.filters&&(r=G.process(e.filters,r,n)),e.sortBy&&(r=Z.process(e.sortBy,r,n)),r}},fe=class{#e;constructor(e){this.#e=[],this.effects=[],e&&(this.#e=e)}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},pe=class e{constructor(t){this.definitions=[],this.rules=[],t&&(e.validateDefinitions(t),this.definitions=t)}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(B(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}hasRule(e){return this.rules.some(t=>typeof e==`object`?t.id===e.id:t.id===e)}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new L(I.RULE_NOT_FOUND,{identifier:e});return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[z,B,R,H],n=new Set;return e.forEach(e=>{if(e.id===void 0&&z(e)===!1)throw new L(I.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new L(I.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new L(I.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},me=class{constructor({rules:e,effects:t}){this.list=new pe(e),this.effects=new fe(t)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.effects.hydrateDefinitions(e,t)}onChange(e,t){this.effects.onChange(e,t)}},he=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new ae,this.#i=new Q,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f,this.#a=new me({rules:t,effects:n}),this.#a.hydrateDefinitions(e??[],l);let S={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:ie()};this.search=new Y({initialSearchTerm:r},S),this.filters=new G({initialFilters:c},S),this.sortBy=new Z({initialSortBy:i,initialSortDirection:a},S),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},S),this.pagination=new q({page:u,numItemsPerPage:d},S),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(N.CHANGE,b),y&&this.#r.on(N.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(N.READY,v))}#o(e){if(this.#r.isSilenced)return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(N.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:P.CORE,event:N.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(N.READY,{source:P.CORE,event:N.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Q.test({mixins:t,items:this.items,context:this.context})}return Q.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilenced}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if(l(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:P.CORE,event:N.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:P.CORE,event:N.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){e!==void 0&&l(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){if(e===void 0||Object.is(e,this.context))return;let t=this.context;l(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},ge=class{#e;constructor(e,t){let n=()=>this;this.#e=new he(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:e,rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w,controllerRef:T,children:E}){let[D,O]=a(Date.now()),[k]=a(()=>{let n=new ge(e,{rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w});return n.events.on(N.SYNC_UPDATED_AT,e=>O(e)),n});k.setItems(e),k.setIsLoading(m),k.setIsDisabled(h),k.setRules(t),k.setContext(p),k.pagination.setPage(g),k.pagination.setNumItemsPerPage(_),n(T,()=>k,[k]);let A=r(()=>[k,D],[k,D]);return o(C.Provider,{value:A,children:E})}$.Content=T,$.SearchTermHaystack=re;function _e(){return i(null)}export{$ as Finder,M as StringMatch,g as filterRule,m as finderRuleset,v as groupByRule,y as ruleEffect,h as searchRule,_ as sortByRule,x as transformFilterToBoolean,S as transformFilterToMultiple,b as transformFilterToSingleValue,w as useFinder,_e as useFinderRef};
|
package/package.json
CHANGED