{"version":3,"file":"unhead-angular.mjs","sources":["../../src/context.ts","../../src/composables.ts","../../src/head.component.ts","../../src/public-api.ts","../../src/unhead-angular.ts"],"sourcesContent":["import type { Unhead } from 'unhead/types'\nimport { InjectionToken } from '@angular/core'\n\nexport const headSymbol = 'usehead'\n\nexport const UnheadInjectionToken = new InjectionToken<Unhead>(headSymbol)\n","import type { ActiveHeadEntry, HeadEntryOptions, HeadSafe, Unhead, UseHeadInput, UseScriptInput, UseScriptOptions, UseScriptReturn, UseSeoMetaInput } from 'unhead/types'\nimport { DestroyRef, effect, inject } from '@angular/core'\nimport { useHead as baseHead, useHeadSafe as baseHeadSafe, useSeoMeta as baseSeoMeta, useScript as baseUseScript } from 'unhead'\nimport { UnheadInjectionToken } from './context'\n\nexport function useUnhead() {\n  const instance = inject<Unhead>(UnheadInjectionToken)\n  if (!instance) {\n    throw new Error('useHead() was called without proper injection context.')\n  }\n  return instance\n}\n\nfunction withSideEffects<T extends ActiveHeadEntry<any>>(input: any, options: any, fn: any): T {\n  const head = options.head || useUnhead()\n  const entry = fn(head, input, options)\n\n  const destroyRef = inject(DestroyRef)\n  destroyRef.onDestroy(() => {\n    entry.dispose()\n  })\n\n  return entry\n}\n\nexport function useHead(input: UseHeadInput = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<UseHeadInput> {\n  return withSideEffects(input, options, baseHead)\n}\n\nexport function useHeadSafe(input: HeadSafe = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<HeadSafe> {\n  return withSideEffects<ActiveHeadEntry<HeadSafe>>(input, options, baseHeadSafe)\n}\n\nexport function useSeoMeta(input: UseSeoMetaInput = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<UseSeoMetaInput> {\n  return withSideEffects<ActiveHeadEntry<UseSeoMetaInput>>(input, options, baseSeoMeta)\n}\n\nexport function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T> {\n  const input = (typeof _input === 'string' ? { src: _input } : _input) as UseScriptInput\n  const options = _options || {} as UseScriptOptions<T>\n  const head = options?.head || useUnhead()\n  options.head = head\n\n  const mountCbs: (() => void)[] = []\n  const sideEffects: (() => void)[] = []\n  let isMounted = false\n\n  const destroyRef = inject(DestroyRef)\n\n  effect(() => {\n    isMounted = true\n    // drain so a re-invoked effect does not run the same callback twice\n    mountCbs.splice(0).forEach(i => i())\n  })\n\n  if (typeof options.trigger === 'undefined') {\n    options.trigger = (load) => {\n      if (isMounted) {\n        load()\n      }\n      else {\n        mountCbs.push(load)\n      }\n    }\n  }\n\n  // @ts-expect-error untyped\n  const script = baseUseScript(head, input as BaseUseScriptInput, options)\n  // capture the controller at registration time so destroy aborts the controller\n  // that was active when this component registered, not a newer one\n  const triggerAbortController = script._triggerAbortController\n\n  // core's onLoaded/onError register by identity and return an identity-based\n  // disposer; we defer registration to mount and tie the disposer to destroy\n  // core returns an identity-based disposer at runtime although the type says void\n  const baseOnLoaded = script.onLoaded as unknown as (cb: any) => (() => void) | undefined\n  const baseOnError = script.onError as unknown as (cb: any) => (() => void) | undefined\n  const _registerCb = (register: () => (() => void) | undefined) => {\n    let disposed = false\n    let off: (() => void) | undefined\n    const run = () => {\n      if (disposed)\n        return\n      off = register() ?? (() => {})\n      sideEffects.push(off)\n    }\n    if (isMounted)\n      run()\n    else\n      mountCbs.push(run)\n    return () => {\n      if (disposed)\n        return\n      disposed = true\n      const idx = mountCbs.indexOf(run)\n      if (idx !== -1)\n        mountCbs.splice(idx, 1)\n      off?.()\n    }\n  }\n\n  destroyRef.onDestroy(() => {\n    isMounted = false\n    triggerAbortController?.abort()\n    sideEffects.forEach(i => i())\n  })\n  script.onLoaded = (cb: (instance: T) => void | Promise<void>) => _registerCb(() => baseOnLoaded(cb))\n  script.onError = (cb: (err?: Error) => void | Promise<void>) => _registerCb(() => baseOnError(cb))\n  return script\n}\n","import type { OnDestroy } from '@angular/core'\nimport { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'\nimport { useHead } from './composables'\n\ninterface NodeProps {\n  type: string | symbol\n  props?: Record<string, any>\n  children?: NodeProps[]\n}\n\n@Component({\n  selector: 'unhead-head',\n  standalone: true,\n  template: '<ng-content></ng-content>',\n  schemas: [CUSTOM_ELEMENTS_SCHEMA],\n})\nexport class Head implements OnDestroy {\n  private headEntry = useHead({})\n\n  updateHead(nodes: NodeProps[]) {\n    const transformed = this.transformNodes(nodes)\n    this.headEntry.patch(transformed)\n  }\n\n  private transformNodes(nodes: NodeProps[]) {\n    const result: Record<string, any> = {}\n\n    const processNode = (node: NodeProps) => {\n      if (typeof node.type === 'symbol') {\n        node.children?.forEach(child => processNode(child as NodeProps))\n        return\n      }\n\n      const type = node.type as string\n      if (node.children?.length === 1) {\n        result[type] = node.children[0]\n      }\n      else if (Object.keys(node.props || {}).length) {\n        result[type] = result[type] || []\n        result[type].push(node.props)\n      }\n    }\n\n    nodes.forEach(processNode)\n    return result\n  }\n\n  ngOnDestroy() {\n    this.headEntry.dispose()\n  }\n}\n","/*\n * Public API Surface of unhead\n */\n\nexport { useHead, useHeadSafe, useScript, useSeoMeta, useUnhead } from './composables'\nexport { headSymbol, UnheadInjectionToken } from './context'\nexport { Head } from './head.component'\nexport { defineLink, defineScript } from 'unhead'\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":["baseHead","baseHeadSafe","baseSeoMeta","baseUseScript"],"mappings":";;;;;AAGO,MAAM,UAAU,GAAG;MAEb,oBAAoB,GAAG,IAAI,cAAc,CAAS,UAAU;;SCAzD,SAAS,GAAA;AACvB,IAAA,MAAM,QAAQ,GAAG,MAAM,CAAS,oBAAoB,CAAC;IACrD,IAAI,CAAC,QAAQ,EAAE;AACb,QAAA,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC;IAC3E;AACA,IAAA,OAAO,QAAQ;AACjB;AAEA,SAAS,eAAe,CAAiC,KAAU,EAAE,OAAY,EAAE,EAAO,EAAA;IACxF,MAAM,IAAI,GAAG,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