import { r as registerInstance, c as createEvent, h, d as Host, a as getElement } from './index-DtIEDtZ8.js';
import { g as getIcon } from './icons-e2ZcgK7p.js';
import { d as detectFramework, t as trackComponent } from './framework-detection-DcmcuUOA.js';
import { i as isNestedInIfxComponent } from './dom-utils-Bw2fh5LT.js';

const infineonIconStencilCss = "ifx-icon{display:inline-flex;justify-content:center}ifx-icon:empty{display:none}";

const InfineonIconStencil = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.consoleError = createEvent(this, "consoleError", 7);
        this.icon = "";
    }
    updateIcon(newIcon) {
        this.internalIcon = newIcon;
        this.setIcon();
    }
    convertStringToHtml(htmlString) {
        const div = document.createElement('div');
        div.innerHTML = htmlString;
        return div.firstChild;
    }
    convertHtmlToObject(htmlElement) {
        let pathToObject = Array
            .from(htmlElement.attributes, ({ name, value }) => ({ name, value }))
            .reduce((acc, current) => {
            acc[current.name] = current.value;
            return acc;
        }, {});
        return pathToObject;
    }
    convertPathsToVnode(htmlPath) {
        let svgPaths = [];
        const parentPath = this.convertHtmlToObject(htmlPath);
        const parentPathToVnode = h("path", parentPath);
        svgPaths.push(parentPathToVnode);
        if (htmlPath.firstChild) {
            const paths = htmlPath.querySelectorAll('path');
            const pathLength = htmlPath.querySelectorAll('path').length;
            for (let i = 0; i < pathLength; i++) {
                let pathToObject = this.convertHtmlToObject(paths[i]);
                let objToVnode = h("path", pathToObject);
                svgPaths.push(objToVnode);
            }
        }
        return svgPaths;
    }
    getSVG(svgPath) {
        const htmlPath = this.convertStringToHtml(this.ifxIcon);
        const width = htmlPath.getAttribute('width');
        const height = htmlPath.getAttribute('height');
        const fill = htmlPath.getAttribute('fill');
        const viewBox = htmlPath.getAttribute('viewBox');
        return h("svg", { class: "inline-svg", width: width, height: height, xmlns: "http://www.w3.org/2000/svg", fill: fill, viewBox: viewBox }, ...svgPath);
    }
    constructIcon() {
        if (this.ifxIcon) {
            const htmlPath = this.convertStringToHtml(this.ifxIcon);
            const svgPath = this.convertPathsToVnode(htmlPath);
            const SVG = this.getSVG(svgPath);
            return SVG;
        }
        else if (this.icon !== "") {
            this.consoleError.emit(true);
            return;
        }
        else {
            return;
        }
    }
    setIcon() {
        const toCamelCase = str => str
            .replace(/[-_]+(.)/g, (_, chr) => chr.toUpperCase()) // handle - and _ to uppercase
            .replace(/^(.)/, (m) => m.toLowerCase()); // ensure first letter is lowercase
        const iconName = toCamelCase(this.internalIcon);
        this.ifxIcon = getIcon(iconName);
    }
    isInsideAgGrid(el) {
        var _a;
        let current = el;
        while (current) {
            if ((_a = current.className) === null || _a === void 0 ? void 0 : _a.toLowerCase().startsWith('ag-')) {
                return true;
            }
            current = current.parentElement;
        }
        return false;
    }
    isInsideChoices(el) {
        var _a;
        let current = el;
        while (current) {
            if ((_a = current.className) === null || _a === void 0 ? void 0 : _a.toLowerCase().startsWith('choices__')) {
                return true;
            }
            current = current.parentElement;
        }
        return false;
    }
    componentWillLoad() {
        this.internalIcon = this.icon;
        this.setIcon();
    }
    async componentDidLoad() {
        if (!isNestedInIfxComponent(this.el)) {
            if (!this.isInsideAgGrid(this.el) && !this.isInsideChoices(this.el)) {
                const framework = detectFramework();
                trackComponent('ifx-icon', await framework);
            }
        }
    }
    render() {
        return (h(Host, { key: 'e5cab8793c55383813b83e97038545526070d0a7' }, this.constructIcon()));
    }
    get el() { return getElement(this); }
    static get watchers() { return {
        "icon": ["updateIcon"]
    }; }
};
InfineonIconStencil.style = infineonIconStencilCss;

export { InfineonIconStencil as ifx_icon };
//# sourceMappingURL=ifx-icon.entry.js.map

//# sourceMappingURL=ifx-icon.entry.js.map