import dompurify from "dompurify";
import { LitElement, css, svg as external_lit_svg } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { guard } from "lit/directives/guard.js";
import { unsafeSVG } from "lit/directives/unsafe-svg.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { watch } from "../../../decorators/watch.js";
import { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { parseAssetURL } from "../../../utils/asset-path-utils.js";
import { fetchIcon } from "./fetch-icon.js";
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicIcon extends LightDomMixin(InitializeBindingsMixin(LitElement)) {
    async fetchIcon(url) {
        try {
            return await fetchIcon(url);
        } catch (e) {
            this.error = e;
            this.requestUpdate();
            return null;
        }
    }
    validateSVG(svg) {
        if (!/^<svg[\s\S]+<\/svg>$/gm.test(svg)) this.bindings.engine.logger.warn('The inline "icon" prop is not an svg element. You may encounter rendering issues.');
    }
    async getIcon() {
        if (!this.bindings) return null;
        const url = parseAssetURL(this.icon, this.bindings.store.state.iconAssetsPath);
        const svg = url ? await this.fetchIcon(url) : this.icon;
        if (svg) this.validateSVG(svg);
        const sanitizedSvg = svg ? dompurify.sanitize(svg, {
            USE_PROFILES: {
                svg: true,
                svgFilters: true
            }
        }) : null;
        return sanitizedSvg;
    }
    async updateIcon() {
        const svgPromise = this.getIcon();
        this.svg = await svgPromise;
    }
    async initialize() {
        await this.updateIcon();
    }
    render() {
        this.ariaHidden = 'true';
        return external_lit_svg`${guard([
            this.svg
        ], ()=>unsafeSVG(this.svg))}`;
    }
    constructor(...args){
        super(...args), this.svg = null;
    }
}
AtomicIcon.styles = css`@layer components{atomic-icon{aspect-ratio:1/1;display:inline-block;fill:currentColor;height:auto;@supports not (aspect-ratio:1/1){height:auto}>svg{aspect-ratio:1/1;height:auto;max-height:100%;width:100%}}}`;
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicIcon.prototype, "icon", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof AnyBindings ? Object : AnyBindings)
], AtomicIcon.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicIcon.prototype, "error", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Object)
], AtomicIcon.prototype, "svg", void 0);
_ts_decorate([
    watch('icon'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", Promise)
], AtomicIcon.prototype, "updateIcon", null);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIcon.prototype, "render", null);
AtomicIcon = _ts_decorate([
    customElement('atomic-icon')
], AtomicIcon);
export { AtomicIcon };
