"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFromIconfontCN = void 0;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = tslib_1.__importDefault(require("react"));
const icon_1 = require("./icon");
const util_1 = require("./util");
// source code from '@ant-design/icons'
const customCache = new Set();
function isValidCustomScriptUrl(scriptUrl) {
    return Boolean(typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl));
}
function createScriptUrlElements(scriptUrls, index = 0) {
    const currentScriptUrl = scriptUrls[index];
    if (isValidCustomScriptUrl(currentScriptUrl)) {
        const script = document.createElement('script');
        script.setAttribute('src', currentScriptUrl);
        script.setAttribute('data-namespace', currentScriptUrl);
        if (scriptUrls.length > index + 1) {
            script.onload = () => {
                createScriptUrlElements(scriptUrls, index + 1);
            };
            script.onerror = () => {
                createScriptUrlElements(scriptUrls, index + 1);
            };
        }
        customCache.add(currentScriptUrl);
        document.body.appendChild(script);
    }
}
const svgBaseProps = {
    width: '1em',
    height: '1em',
    fill: 'currentColor',
};
function createFromIconfontCN(options = {}) {
    const { scriptUrl, extraCommonProps = {} } = options;
    /**
     * DOM API required.
     * Make sure in browser environment.
     * The Custom Icon will create a <script/>
     * that loads SVG symbols and insert the SVG Element into the document body.
     */
    if (scriptUrl &&
        typeof document !== 'undefined' &&
        typeof window !== 'undefined' &&
        typeof document.createElement === 'function') {
        if (Array.isArray(scriptUrl)) {
            // 因为iconfont资源会把svg插入before，所以前加载相同type会覆盖后加载，为了数组覆盖顺序，倒叙插入
            createScriptUrlElements(scriptUrl.reverse());
        }
        else {
            createScriptUrlElements([scriptUrl]);
        }
    }
    const IconFont = react_1.default.forwardRef((props, ref) => {
        const { icon, children, rotate, anim, fill, className = '' } = props, restProps = tslib_1.__rest(props, ["icon", "children", "rotate", "anim", "fill", "className"]);
        const iconShapeOptions = { rotate, anim, fill };
        // children > icon
        let content = null;
        if (icon) {
            content = (react_1.default.createElement("svg", Object.assign({}, svgBaseProps, { focusable: 'false' }),
                react_1.default.createElement("use", { xlinkHref: `#${icon}` })));
        }
        if (children) {
            content = children;
        }
        const iconShapeClx = (0, util_1.getIconShapeClxList)(iconShapeOptions);
        return (react_1.default.createElement(icon_1._InternalIcon, Object.assign({}, extraCommonProps, restProps, { className: (0, classnames_1.default)(className, iconShapeClx), ref: ref }), content));
    });
    IconFont.displayName = 'Iconfont';
    return IconFont;
}
exports.createFromIconfontCN = createFromIconfontCN;
//# sourceMappingURL=iconfont-cn.js.map