{"version":3,"file":"ngxtension-inject-attribute.mjs","sources":["../../../../libs/ngxtension/inject-attribute/src/inject-attribute.ts","../../../../libs/ngxtension/inject-attribute/src/ngxtension-inject-attribute.ts"],"sourcesContent":["import { HostAttributeToken, inject, type Injector } from '@angular/core';\nimport { assertInjector } from 'ngxtension/assert-injector';\n\n/**\n * Options for the `injectAttribute` function.\n */\nexport type InjectAttributeOptions<T = unknown> = {\n\t/**\n\t * Custom injector to use for dependency injection.\n\t */\n\tinjector?: Injector;\n\t/**\n\t * Transform function to convert the string attribute value to the desired type.\n\t * This is useful for type coercion (e.g., string to number or boolean).\n\t */\n\ttransform?: (value: string) => T;\n};\n\n//INSPIRED BY @netbasal ARTICLE https://medium.com/netanelbasal/streamlining-attribute-injection-in-angular-the-hostattributetoken-approach-494f5c1428b8\n\n/**\n * Injects the value of a host attribute with a default value if the attribute is not present.\n * This is a simplified wrapper around Angular's `HostAttributeToken`\n *\n * @template T - The type of the attribute value\n * @param {string} key - The name of the attribute to inject\n * @param {T} defaultValue - The default value to return if the attribute is not present\n * @param {InjectAttributeOptions} options - Optional configuration for the injector\n * @returns {T} The attribute value or the default value\n *\n * @example\n * ```ts\n * export class Button {\n *   variation = injectAttribute<'primary' | 'secondary'>('variation', 'primary');\n * }\n * ```\n *\n * @example\n * <app-button variation=\"secondary\" />\n */\nexport function injectAttribute<T>(\n\tkey: string,\n\tdefaultValue: T,\n\toptions?: InjectAttributeOptions<T>,\n): T {\n\treturn assertInjector(injectAttribute, options?.injector, () => {\n\t\tconst value = inject(new HostAttributeToken(key), { optional: true });\n\n\t\tif (value == null) {\n\t\t\treturn defaultValue;\n\t\t}\n\n\t\tif (options?.transform) {\n\t\t\treturn options.transform(value);\n\t\t}\n\n\t\treturn value as T;\n\t});\n}\n\n/**\n * Injects the value of a host attribute that must be present.\n * Throws an error if the attribute is not provided.\n *\n * @template T - The type of the attribute value\n * @param {string} key - The name of the attribute to inject\n * @param {InjectAttributeOptions} options - Optional configuration for the injector\n * @returns {T} The attribute value\n * @throws {Error} If the attribute is not present in the host element\n */\nexport namespace injectAttribute {\n\texport function required<T>(\n\t\tkey: string,\n\t\toptions?: InjectAttributeOptions<T>,\n\t): T {\n\t\treturn assertInjector(required, options?.injector, () => {\n\t\t\tconst value = inject(new HostAttributeToken(key));\n\t\t\treturn options?.transform ? options.transform(value) : (value as T);\n\t\t});\n\t}\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAkBA;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;SACa,eAAe,CAC9B,GAAW,EACX,YAAe,EACf,OAAmC,EAAA;IAEnC,OAAO,cAAc,CAAC,eAAe,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAK;AAC9D,QAAA,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,kBAAkB,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAEtE,QAAA,IAAI,KAAK,IAAI,IAAI,EAAE;AAClB,YAAA,OAAO,YAAY,CAAC;SACpB;AAED,QAAA,IAAI,OAAO,EAAE,SAAS,EAAE;AACvB,YAAA,OAAO,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;SAChC;AAED,QAAA,OAAO,KAAU,CAAC;AACnB,KAAC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;AASG;AACH,CAAA,UAAiB,eAAe,EAAA;AAC/B,IAAA,SAAgB,QAAQ,CACvB,GAAW,EACX,OAAmC,EAAA;QAEnC,OAAO,cAAc,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAK;YACvD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC;AAClD,YAAA,OAAO,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,GAAI,KAAW,CAAC;AACrE,SAAC,CAAC,CAAC;KACH;AARe,IAAA,eAAA,CAAA,QAAQ,WAQvB,CAAA;AACF,CAAC,EAVgB,eAAe,KAAf,eAAe,GAU/B,EAAA,CAAA,CAAA;;AChFD;;AAEG;;;;"}