{"version":3,"file":"primeng-usestyle.mjs","sources":["../../src/usestyle/usestyle.ts","../../src/usestyle/primeng-usestyle.ts"],"sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport { Injectable, inject } from '@angular/core';\nimport { setAttribute, setAttributes } from '@primeuix/utils';\n\nlet _id = 0;\n\n@Injectable({ providedIn: 'root' })\nexport class UseStyle {\n    document: Document = inject(DOCUMENT);\n\n    private styleSheets: Map<string, CSSStyleSheet> = new Map();\n\n    private shadowRoots: Set<ShadowRoot> = new Set();\n\n    /**\n     * Applies the injected styles inside the given shadow root.\n     */\n    addShadowRoot(shadowRoot: ShadowRoot): () => void {\n        if (!this.isAdoptionSupported() || !(shadowRoot instanceof ShadowRoot)) return () => {};\n\n        if (!this.shadowRoots.has(shadowRoot)) {\n            this.shadowRoots.add(shadowRoot);\n\n            shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, ...[...this.styleSheets.values()].filter((styleSheet) => !shadowRoot.adoptedStyleSheets.includes(styleSheet))];\n        }\n\n        return () => this.removeShadowRoot(shadowRoot);\n    }\n\n    /**\n     * Stops applying the injected styles inside the given shadow root.\n     */\n    removeShadowRoot(shadowRoot: ShadowRoot): void {\n        if (!this.shadowRoots.delete(shadowRoot)) return;\n\n        const styleSheets = new Set(this.styleSheets.values());\n\n        shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter((styleSheet) => !styleSheets.has(styleSheet));\n    }\n\n    /**\n     * Removes an injected style from the document and from the shadow roots that adopted it.\n     */\n    remove(name: string): void {\n        const styleSheet = this.styleSheets.get(name);\n\n        this.styleSheets.delete(name);\n        this.document?.querySelector(`style[data-primeng-style-id=\"${name}\"]`)?.remove();\n\n        if (!styleSheet) return;\n\n        this.shadowRoots.forEach((shadowRoot) => {\n            shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter((adopted) => adopted !== styleSheet);\n        });\n    }\n\n    use(css, options: any = {}) {\n        let isLoaded = false;\n        let cssRef = css;\n        let styleRef: HTMLStyleElement | null = null;\n\n        const { immediate = true, manual = false, name = `style_${++_id}`, id = undefined, media = undefined, nonce = undefined, first = false, variables = false, props = {} } = options;\n\n        if (!this.document) return;\n\n        styleRef = (this.document.querySelector(`style[data-primeng-style-id=\"${name}\"]`) || (id && this.document.getElementById(id)) || this.document.createElement('style')) as HTMLStyleElement;\n\n        if (styleRef) {\n            if (!styleRef.isConnected) {\n                cssRef = css;\n\n                const HEAD = this.document.head;\n\n                setAttribute(styleRef, 'nonce', nonce);\n\n                first && HEAD.firstChild ? HEAD.insertBefore(styleRef, HEAD.firstChild) : HEAD.appendChild(styleRef);\n                setAttributes(styleRef, {\n                    type: 'text/css',\n                    media,\n                    nonce,\n                    'data-primeng-style-id': name\n                });\n            }\n\n            if (styleRef.textContent !== cssRef) {\n                styleRef.textContent = cssRef;\n            }\n        }\n\n        if (!variables) {\n            this.adoptStyleSheet(name, cssRef ?? '', first);\n        }\n\n        return {\n            id,\n            name,\n            el: styleRef,\n            css: cssRef\n        };\n    }\n\n    private adoptStyleSheet(name: string, css: string, first: boolean): void {\n        if (!this.isAdoptionSupported()) return;\n\n        const styleSheet = this.styleSheets.get(name);\n\n        if (styleSheet) {\n            styleSheet.replaceSync(css);\n\n            return;\n        }\n\n        const added = new CSSStyleSheet();\n\n        added.replaceSync(css);\n\n        this.styleSheets = first ? new Map([[name, added], ...this.styleSheets]) : this.styleSheets.set(name, added);\n\n        this.shadowRoots.forEach((shadowRoot) => {\n            shadowRoot.adoptedStyleSheets = first ? [added, ...shadowRoot.adoptedStyleSheets] : [...shadowRoot.adoptedStyleSheets, added];\n        });\n    }\n\n    private isAdoptionSupported(): boolean {\n        return typeof ShadowRoot !== 'undefined' && typeof CSSStyleSheet !== 'undefined' && typeof CSSStyleSheet.prototype.replaceSync === 'function';\n    }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;AAIA,IAAI,GAAG,GAAG,CAAC;MAGE,QA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