/**
 * amis v3.1.0
 * Copyright 2018-2023 baidu
 */

import cloneDeep from 'lodash/cloneDeep';
import { getDefaultLocale, makeTranslator } from 'amis-core';

var svgIcons = [];
function getSvgMountNode() {
    var node = document.getElementById('amis-icon-manage-mount-node');
    if (node) {
        return node;
    }
    else {
        var newNode = document.createElement('div');
        newNode.setAttribute('id', 'amis-icon-manage-mount-node');
        newNode.setAttribute('style', 'width:0;height:0;visibility:hidden;');
        if (document.body.firstElementChild) {
            document.body.insertBefore(newNode, document.body.firstElementChild);
        }
        else {
            document.body.appendChild(newNode);
        }
        return newNode;
    }
}
function mountIconSpiriteToDom(spirite) {
    var node = getSvgMountNode();
    node && (node.innerHTML = spirite);
}
var refreshIconList = null;
function setRefreshSvgListAction(func) {
    if (func && typeof func === 'function') {
        refreshIconList = function () {
            return func({
                setSvgIconList: setSvgIconList,
                mountIconSpiriteToDom: mountIconSpiriteToDom
            });
        };
    }
    else {
        refreshIconList = null;
        throw new Error('setRefreshSvgListAction need a function param, not ' + typeof func);
    }
}
function setSvgIconList(groups, combine, local) {
    if (combine === void 0) { combine = true; }
    if (local === void 0) { local = getDefaultLocale(); }
    var clonedIcons = cloneDeep(groups);
    var __ = makeTranslator(local);
    if (combine) {
        var allIcons = clonedIcons
            .map(function (item) { return item.children; })
            .flat();
        svgIcons = [
            {
                name: __('IconSelect.all'),
                groupId: 'all',
                children: allIcons
            }
        ].concat(groups);
    }
    else {
        svgIcons = groups;
    }
}

export { mountIconSpiriteToDom, refreshIconList, setRefreshSvgListAction, setSvgIconList, svgIcons };
