{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-id/index.ts"],"sourcesContent":["import { computed, getCurrentInstance, inject, unref } from 'vue'\r\nimport { isClient } from '@vueuse/core'\r\nimport { debugWarn } from '@sgui-plus/utils/error'\r\n\r\nimport type { InjectionKey, Ref } from 'vue'\r\nimport type { MaybeRef } from '@vueuse/core'\r\n\r\nexport type SgIdInjectionContext = {\r\n  prefix: number\r\n  current: number\r\n}\r\n\r\nconst defaultIdInjection = {\r\n  prefix: Math.floor(Math.random() * 10000),\r\n  current: 0,\r\n}\r\n\r\nexport const ID_INJECTION_KEY: InjectionKey<SgIdInjectionContext> =\r\n  Symbol('sgIdInjection')\r\n\r\n\r\nexport const useIdInjection = (): SgIdInjectionContext => {\r\n  return getCurrentInstance()\r\n    ? inject(ID_INJECTION_KEY, defaultIdInjection)\r\n    : defaultIdInjection\r\n}\r\n\r\nexport const useId = (deterministicId?: MaybeRef<string>): Ref<string> => {\r\n  const idInjection = inject(ID_INJECTION_KEY, defaultIdInjection)\r\n\r\n  if (!isClient && idInjection === defaultIdInjection) {\r\n    debugWarn(\r\n      'IdInjection',\r\n      `Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed\r\nusage: app.provide(ID_INJECTION_KEY, {\r\n  prefix: number,\r\n  current: number,\r\n})`\r\n    )\r\n  }\r\n\r\n  const idRef = computed(\r\n    () =>\r\n      unref(deterministicId) ||\r\n      `sg-id-${idInjection.prefix}-${idInjection.current++}`\r\n  )\r\n\r\n  return idRef\r\n}\r\n"],"names":[],"mappings":";;;;AAGA,MAAM,kBAAkB,GAAG;AAC3B,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC;AACzC,EAAE,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AACU,MAAC,gBAAgB,GAAG,MAAM,CAAC,eAAe,EAAE;AAC5C,MAAC,cAAc,GAAG,MAAM;AACpC,EAAE,OAAO,kBAAkB,EAAE,GAAG,MAAM,CAAC,gBAAgB,EAAE,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;AAClG,EAAE;AACU,MAAC,KAAK,GAAG,CAAC,eAAe,KAAK;AAC1C,EAAE,MAAM,WAAW,GAAG,MAAM,CAAC,gBAAgB,EAAE,kBAAkB,CAAC,CAAC;AACnE,EAAE,IAAI,CAAC,QAAQ,IAAI,WAAW,KAAK,kBAAkB,EAAE;AACvD,IAAI,SAAS,CAAC,aAAa,EAAE,CAAC;AAC9B;AACA;AACA;AACA,EAAE,CAAC,CAAC,CAAC;AACL,GAAG;AACH,EAAE,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AACjH,EAAE,OAAO,KAAK,CAAC;AACf;;;;"}