{"version":3,"file":"collection.mjs","sources":["../../../../../../packages/components/collection/src/collection.ts"],"sourcesContent":["// @ts-nocheck\r\nimport { inject, onBeforeUnmount, onMounted, provide, ref, unref } from 'vue'\r\nimport Collection from './collection.vue'\r\nimport CollectionItem from './collection-item.vue'\r\n\r\nimport type { InjectionKey, SetupContext } from 'vue'\r\nimport type {\r\n  SgCollectionInjectionContext,\r\n  SgCollectionItemInjectionContext,\r\n} from './tokens'\r\n\r\nexport const COLLECTION_ITEM_SIGN = `data-el-collection-item`\r\n\r\n// Make sure the first letter of name is capitalized\r\nexport const createCollectionWithScope = (name: string) => {\r\n  const COLLECTION_NAME = `Sg${name}Collection`\r\n  const COLLECTION_ITEM_NAME = `${COLLECTION_NAME}Item`\r\n  const COLLECTION_INJECTION_KEY: InjectionKey<SgCollectionInjectionContext> =\r\n    Symbol(COLLECTION_NAME)\r\n  const COLLECTION_ITEM_INJECTION_KEY: InjectionKey<SgCollectionItemInjectionContext> =\r\n    Symbol(COLLECTION_ITEM_NAME)\r\n\r\n  const SgCollection = {\r\n    ...Collection,\r\n    name: COLLECTION_NAME,\r\n    setup() {\r\n      const collectionRef = ref<HTMLElement | null>(null)\r\n      const itemMap: SgCollectionInjectionContext['itemMap'] = new Map()\r\n      const getItems = () => {\r\n        const collectionEl = unref(collectionRef)\r\n\r\n        if (!collectionEl) return []\r\n        const orderedNodes = Array.from(\r\n          collectionEl.querySelectorAll(`[${COLLECTION_ITEM_SIGN}]`)\r\n        )\r\n\r\n        const items = [...itemMap.values()]\r\n\r\n        return items.sort(\r\n          (a, b) => orderedNodes.indexOf(a.ref!) - orderedNodes.indexOf(b.ref!)\r\n        )\r\n      }\r\n\r\n      provide(COLLECTION_INJECTION_KEY, {\r\n        itemMap,\r\n        getItems,\r\n        collectionRef,\r\n      })\r\n    },\r\n  }\r\n\r\n  const SgCollectionItem = {\r\n    ...CollectionItem,\r\n    name: COLLECTION_ITEM_NAME,\r\n    setup(_: unknown, { attrs }: SetupContext) {\r\n      const collectionItemRef = ref<HTMLElement | null>(null)\r\n      const collectionInjection = inject(COLLECTION_INJECTION_KEY, undefined)!\r\n\r\n      provide(COLLECTION_ITEM_INJECTION_KEY, {\r\n        collectionItemRef,\r\n      })\r\n\r\n      onMounted(() => {\r\n        const collectionItemEl = unref(collectionItemRef)\r\n        if (collectionItemEl) {\r\n          collectionInjection.itemMap.set(<HTMLElement>collectionItemEl, {\r\n            ref: <HTMLElement>collectionItemEl,\r\n            ...attrs,\r\n          })\r\n        }\r\n      })\r\n\r\n      onBeforeUnmount(() => {\r\n        const collectionItemEl = unref(collectionItemRef)!\r\n        collectionInjection.itemMap.delete(<HTMLElement>collectionItemEl)\r\n      })\r\n    },\r\n  }\r\n\r\n  return {\r\n    COLLECTION_INJECTION_KEY,\r\n    COLLECTION_ITEM_INJECTION_KEY,\r\n    SgCollection,\r\n    SgCollectionItem,\r\n  }\r\n}\r\n"],"names":[],"mappings":";;;;AAGY,MAAC,oBAAoB,GAAG,CAAC,uBAAuB,EAAE;AAClD,MAAC,yBAAyB,GAAG,CAAC,IAAI,KAAK;AACnD,EAAE,MAAM,eAAe,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;AAChD,EAAE,MAAM,oBAAoB,GAAG,CAAC,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC;AACxD,EAAE,MAAM,wBAAwB,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAC3D,EAAE,MAAM,6BAA6B,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACrE,EAAE,MAAM,YAAY,GAAG;AACvB,IAAI,GAAG,UAAU;AACjB,IAAI,IAAI,EAAE,eAAe;AACzB,IAAI,KAAK,GAAG;AACZ,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;AACtC,MAAM,MAAM,OAAO,mBAAmB,IAAI,GAAG,EAAE,CAAC;AAChD,MAAM,MAAM,QAAQ,GAAG,MAAM;AAC7B,QAAQ,MAA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