{"version":3,"file":"index.mjs","sources":["../../../../../../packages/hooks/use-id/index.ts"],"sourcesContent":["import { getCurrentInstance, inject, unref } from 'vue'\nimport { computedEager } from '@vueuse/core'\nimport { debugWarn, isClient } from '@hd-front-end/utils'\nimport { useGetDerivedNamespace } from '../use-namespace'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { InjectionKey, Ref } from 'vue'\n\nexport type ElIdInjectionContext = {\n  prefix: number\n  current: number\n}\n\nconst defaultIdInjection = {\n  prefix: Math.floor(Math.random() * 10000),\n  current: 0,\n}\n\nexport const ID_INJECTION_KEY: InjectionKey<ElIdInjectionContext> =\n  Symbol('elIdInjection')\n\nexport const useIdInjection = (): ElIdInjectionContext => {\n  return getCurrentInstance()\n    ? inject(ID_INJECTION_KEY, defaultIdInjection)\n    : defaultIdInjection\n}\n\nexport const useId = (deterministicId?: MaybeRef<string>): Ref<string> => {\n  const idInjection = useIdInjection()\n  if (!isClient && idInjection === defaultIdInjection) {\n    debugWarn(\n      'IdInjection',\n      `Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed\nusage: app.provide(ID_INJECTION_KEY, {\n  prefix: number,\n  current: number,\n})`\n    )\n  }\n\n  const namespace = useGetDerivedNamespace()\n\n  // NOTE: Here we use `computedEager` to calculate the id value immediately, avoiding inconsistent id generation due to the lazy feature of `computed` when server rendering.\n  const idRef = computedEager(\n    () =>\n      unref(deterministicId) ||\n      `${namespace.value}-id-${idInjection.prefix}-${idInjection.current++}`\n  )\n\n  return idRef\n}\n"],"names":[],"mappings":";;;;AAaA,MAAM,kBAAqB,GAAA;AAAA,EACzB,QAAQ,IAAK,CAAA,KAAA,CAAM,IAAK,CAAA,MAAA,KAAW,GAAK,CAAA;AAAA,EACxC,OAAS,EAAA,CAAA;AACX,CAAA,CAAA;AAEa,MAAA,gBAAA,GACX,OAAO,eAAe,EAAA;AAEjB,MAAM,iBAAiB,MAA4B;AACxD,EAAA,OAAO,kBAAmB,EAAA,GACtB,MAAO,CAAA,gBAAA,EAAkB,kBAAkB,CAC3C,GAAA,kBAAA,CAAA;AACN,EAAA;AAEa,MAAA,KAAA,GAAQ,CAAC,eAAoD,KAAA;AACxE,EAAA,MAAM,cAAc,cAAe,EAAA,CAAA;AAYnC,EAAA,MAAM,YAAY,sBAAuB,EAAA,CAAA;AAGzC,EAAA,MAAM,KAAQ,GAAA,aAAA;AAAA,IACZ,MACE,MAAM,eAAe,CAAA,IACrB,GAAG,SAAU,CAAA,KAAA,CAAA,IAAA,EAAY,WAAY,CAAA,MAAA,CAAA,CAAA,EAAU,WAAY,CAAA,OAAA,EAAA,CAAA,CAAA;AAAA,GAC/D,CAAA;AAEA,EAAO,OAAA,KAAA,CAAA;AACT;;;;"}