{"version":3,"file":"index.mjs","names":["isClient"],"sources":["../../../../../packages/hooks/use-id/index.ts"],"sourcesContent":["import { getCurrentInstance, inject, unref } from 'vue'\nimport { computedEager } from '@vueuse/core'\nimport { debugWarn, isClient } from '@element-plus/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"],"mappings":";;;;;;;AAaA,MAAM,qBAAqB;CACzB,QAAQ,KAAK,MAAM,KAAK,QAAQ,GAAG,IAAM;CACzC,SAAS;CACV;AAED,MAAa,mBACX,OAAO,gBAAgB;AAEzB,MAAa,uBAA6C;AACxD,QAAO,oBAAoB,GACvB,OAAO,kBAAkB,mBAAmB,GAC5C;;AAGN,MAAa,SAAS,oBAAoD;CACxE,MAAM,cAAc,gBAAgB;AACpC,KAAI,CAACA,cAAY,gBAAgB,mBAC/B,WACE,eACA;;;;IAKD;CAGH,MAAM,YAAY,wBAAwB;AAS1C,QANc,oBAEV,MAAM,gBAAgB,IACtB,GAAG,UAAU,MAAM,MAAM,YAAY,OAAO,GAAG,YAAY,YAC9D"}