var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { isStr, isFn, isObj, isPlainObj } from '@designable/shared';
import { observer } from '@formily/reactive-vue';
import { Tooltip } from '@gaoding/gd-antd-plus';
import { usePrefix, useRegistry, useTheme } from '../../hooks';
import './styles.less';
import { computed, defineComponent, onMounted, provide, ref, unref } from 'vue-demi';
import { composeExport } from '@jeamn/formily-components/src/__builtins__';
import { cloneElement, isVNode, useStyle } from '../../shared/';
import { useContext } from '@jeamn/formily-prototypes';
import cls from 'classnames';
var IconSymbol = Symbol();
var isNumSize = function (val) { return /^[\d.]+$/.test(val); };
// IIconWidgetProps
var __IconWidgetInner = defineComponent({
    name: 'DnIconWidget',
    props: {
        tooltip: { type: Object },
        infer: { type: [String, Function, Object] },
        size: { type: [Number, String] },
    },
    inheritAttrs: false,
    emits: ['click'],
    setup: function (props, _a) {
        var attrs = _a.attrs, emit = _a.emit;
        var themeRef = useTheme();
        var IconContextRef = useContext(IconSymbol);
        var registry = useRegistry();
        var prefixRef = usePrefix('icon');
        return function () {
            var size = isNumSize(props.size)
                ? "".concat(props.size, "px")
                : props.size || '1em';
            var style = useStyle();
            var height = (style === null || style === void 0 ? void 0 : style.height) || size;
            var width = (style === null || style === void 0 ? void 0 : style.width) || size;
            var takeIcon = function (infer) {
                var _a;
                if (isStr(infer)) {
                    var finded = registry.getDesignerIcon(infer);
                    if (finded) {
                        return takeIcon(finded);
                    }
                    return <img src={infer} height={height} width={width}/>;
                }
                else if (isFn(infer)) {
                    return (<infer {...{ height: height, width: width, fill: 'currentColor' }} fill="currentColor"></infer>);
                }
                else if (isVNode(infer)) {
                    if (infer.type === 'svg') {
                        var Component = cloneElement(infer, {
                            height: height,
                            width: width,
                            fill: 'currentColor',
                            viewBox: ((_a = infer.props) === null || _a === void 0 ? void 0 : _a.viewBox) || '0 0 1024 1024',
                            focusable: 'false',
                            'aria-hidden': 'true',
                        });
                        return Component;
                    }
                    else if (infer.type === 'path' || infer.type === 'g') {
                        return (<svg viewBox="0 0 1024 1024" height={height} width={width} fill="currentColor" focusable="false" aria-hidden="true">
                {infer}
              </svg>);
                    }
                    return infer;
                }
                else if (isPlainObj(infer)) {
                    var theme = unref(themeRef);
                    if (infer[theme]) {
                        return takeIcon(infer[theme]);
                    }
                    else if (infer['shadow']) {
                        return <IconWidget.ShadowSVG width={width} height={height} content={infer['shadow']}/>;
                    }
                }
                return null;
            };
            var renderTooltips = function (children) {
                var IconContext = unref(IconContextRef);
                if (!isStr(props.infer) && (IconContext === null || IconContext === void 0 ? void 0 : IconContext.tooltip))
                    return children;
                var tooltip = props.tooltip || registry.getDesignerMessage("icons.".concat(props.infer));
                if (tooltip) {
                    var props_1 = isObj(tooltip) ? tooltip : { content: tooltip };
                    var _a = props_1, content_1 = _a.content, _props = __rest(_a, ["content"]);
                    return (<Tooltip showAfter={200} {..._props} v-slots={{ content: function () { return content_1; } }}>
              {children}
            </Tooltip>);
                }
                return children;
            };
            return renderTooltips(<span {...attrs} class={cls(prefixRef.value)} style={{
                    cursor: attrs.onClick ? 'pointer' : style === null || style === void 0 ? void 0 : style.cursor,
                }} onClick={function () { return emit('click'); }}>
          {takeIcon(props.infer)}
        </span>);
        };
    },
});
var IconWidgetInner = observer(__IconWidgetInner);
var ShadowSVG = defineComponent({
    props: {
        width: [Number, String],
        height: [Number, String],
        content: String,
    },
    setup: function (props, _a) {
        var refInstance = ref();
        var width = isNumSize(props.width) ? "".concat(props.width, "px") : props.width;
        var height = isNumSize(props.height) ? "".concat(props.height, "px") : props.height;
        onMounted(function () {
            if (refInstance.value) {
                var root = refInstance.value.attachShadow({
                    mode: 'open',
                });
                root.innerHTML = "<svg viewBox=\"0 0 1024 1024\" style=\"width:".concat(width, ";height:").concat(height, "\">").concat(props.content, "</svg>");
            }
        });
        return function () { return <div ref={refInstance}></div>; };
    },
});
var Provider = defineComponent({
    name: 'IconWidget.Provider',
    inheritAttrs: false,
    props: { tooltip: Boolean },
    setup: function (props, _a) {
        var slots = _a.slots;
        provide(IconSymbol, computed(function () { return props; }));
        return function () { var _a; return (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots); };
    },
});
export var IconWidget = composeExport(IconWidgetInner, {
    ShadowSVG: ShadowSVG,
    Provider: Provider,
});
//# sourceMappingURL=index.jsx.map