{"version":3,"sources":["../../src/use-target-element/normalize-element.ts"],"sourcesContent":["import { isFunction, isString } from '../utils/basic'\nimport { unwrapGettable, unwrapReffable } from '../utils/unwrap'\n\nimport type { MutableRefObject, RefObject } from 'react'\nimport type { Gettable } from '../utils/basic'\n\nexport type AtomBaseTarget = Element | null\nexport type GlobalTarget = Window | Document\n\nexport type BaseTarget<T extends AtomBaseTarget = AtomBaseTarget> =\n  | MutableRefObject<T | undefined | null>\n  | Parameters<Document['querySelector']>[0]\n  | T\n  | undefined\n  | null\n\nexport type ElementTarget<T extends AtomBaseTarget = AtomBaseTarget> = Gettable<BaseTarget<T>> | false\n\nexport function normalizeElement<T extends GlobalTarget>(target: Gettable<T>): T\nexport function normalizeElement<T extends Element | null>(target: Gettable<T | RefObject<T>>): T\nexport function normalizeElement<T extends keyof HTMLElementTagNameMap>(target: Gettable<T>): HTMLElementTagNameMap[T]\nexport function normalizeElement<T extends keyof SVGElementTagNameMap>(target: Gettable<T>): SVGElementTagNameMap[T]\nexport function normalizeElement<\n  A extends AtomBaseTarget = AtomBaseTarget,\n  T extends ElementTarget<A> = ElementTarget<A>,\n>(target: T): A | null\nexport function normalizeElement(target: unknown) {\n  if (isFunction(target)) {\n    return normalizeElement(unwrapGettable(target))\n  }\n\n  const unrefValue = unwrapReffable(target)\n\n  if (\n    [typeof window !== 'undefined' ? window : null, typeof document !== 'undefined' ? document : null]\n      .filter((e) => Boolean(e))\n      .some((e) => e === unrefValue)\n  ) {\n    return unrefValue\n  }\n\n  if (isString(unrefValue)) {\n    return typeof document !== 'undefined' && 'querySelector' in document\n      ? document.querySelector(unrefValue) || null\n      : null\n  }\n\n  if (\n    (\n      [\n        typeof HTMLElement !== 'undefined' ? HTMLElement : null,\n        typeof SVGElement !== 'undefined' ? SVGElement : null,\n      ].filter((e) => Boolean(e)) as (typeof HTMLElement | typeof SVGElement)[]\n    ).some((classConstructor) => unrefValue instanceof classConstructor)\n  ) {\n    return unrefValue\n  }\n\n  return unrefValue || null\n}\n"],"names":["normalizeElement","target","isFunction","unwrapGettable","unrefValue","unwrapReffable","window","document","filter","e","Boolean","some","isString","querySelector","HTMLElement","SVGElement","classConstructor"],"mappings":";;;;+BA0BgBA;;;eAAAA;;;qBA1BqB;sBACU;;;;;;;;AAyBxC,SAASA,iBAAiBC,MAAe;IAC9C,IAAIC,IAAAA,iBAAU,EAACD,SAAS;QACtB,OAAOD,iBAAiBG,IAAAA,sBAAc,EAACF;IACzC;IAEA,IAAMG,aAAaC,IAAAA,sBAAc,EAACJ;IAElC,IACE;QAAC,OAAOK,WAAW,cAAcA,SAAS;QAAM,OAAOC,aAAa,cAAcA,WAAW;KAAK,CAC/FC,MAAM,CAAC,SAACC;eAAMC,QAAQD;OACtBE,IAAI,CAAC,SAACF;eAAMA,MAAML;QACrB;QACA,OAAOA;IACT;IAEA,IAAIQ,IAAAA,eAAQ,EAACR,aAAa;QACxB,OAAO,OAAOG,aAAa,eAAe,mBAAmBA,WACzDA,SAASM,aAAa,CAACT,eAAe,OACtC;IACN;IAEA,IACE,AACE;QACE,OAAOU,gBAAgB,cAAcA,cAAc;QACnD,OAAOC,eAAe,cAAcA,aAAa;KAClD,CAACP,MAAM,CAAC,SAACC;eAAMC,QAAQD;OACxBE,IAAI,CAAC,SAACK;eAAqBZ,AAAU,YAAVA,YAAsBY;QACnD;QACA,OAAOZ;IACT;IAEA,OAAOA,cAAc;AACvB"}