"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IconWidget = void 0;
var shared_1 = require("@designable/shared");
var reactive_vue_1 = require("@formily/reactive-vue");
var gd_antd_plus_1 = require("@gaoding/gd-antd-plus");
var hooks_1 = require("../../hooks");
require("./styles.less");
var vue_demi_1 = require("vue-demi");
var __builtins__1 = require("@jeamn/formily-components/src/__builtins__");
var shared_2 = require("../../shared/");
var formily_prototypes_1 = require("@jeamn/formily-prototypes");
var classnames_1 = __importDefault(require("classnames"));
var IconSymbol = Symbol();
var isNumSize = function (val) { return /^[\d.]+$/.test(val); };
// IIconWidgetProps
var __IconWidgetInner = (0, vue_demi_1.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 = (0, hooks_1.useTheme)();
        var IconContextRef = (0, formily_prototypes_1.useContext)(IconSymbol);
        var registry = (0, hooks_1.useRegistry)();
        var prefixRef = (0, hooks_1.usePrefix)('icon');
        return function () {
            var size = isNumSize(props.size)
                ? "".concat(props.size, "px")
                : props.size || '1em';
            var style = (0, shared_2.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 ((0, shared_1.isStr)(infer)) {
                    var finded = registry.getDesignerIcon(infer);
                    if (finded) {
                        return takeIcon(finded);
                    }
                    return <img src={infer} height={height} width={width}/>;
                }
                else if ((0, shared_1.isFn)(infer)) {
                    return (<infer {...{ height: height, width: width, fill: 'currentColor' }} fill="currentColor"></infer>);
                }
                else if ((0, shared_2.isVNode)(infer)) {
                    if (infer.type === 'svg') {
                        var Component = (0, shared_2.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 ((0, shared_1.isPlainObj)(infer)) {
                    var theme = (0, vue_demi_1.unref)(themeRef);
                    if (infer[theme]) {
                        return takeIcon(infer[theme]);
                    }
                    else if (infer['shadow']) {
                        return <exports.IconWidget.ShadowSVG width={width} height={height} content={infer['shadow']}/>;
                    }
                }
                return null;
            };
            var renderTooltips = function (children) {
                var IconContext = (0, vue_demi_1.unref)(IconContextRef);
                if (!(0, shared_1.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 = (0, shared_1.isObj)(tooltip) ? tooltip : { content: tooltip };
                    var _a = props_1, content_1 = _a.content, _props = __rest(_a, ["content"]);
                    return (<gd_antd_plus_1.Tooltip showAfter={200} {..._props} v-slots={{ content: function () { return content_1; } }}>
              {children}
            </gd_antd_plus_1.Tooltip>);
                }
                return children;
            };
            return renderTooltips(<span {...attrs} class={(0, classnames_1.default)(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 = (0, reactive_vue_1.observer)(__IconWidgetInner);
var ShadowSVG = (0, vue_demi_1.defineComponent)({
    props: {
        width: [Number, String],
        height: [Number, String],
        content: String,
    },
    setup: function (props, _a) {
        var refInstance = (0, vue_demi_1.ref)();
        var width = isNumSize(props.width) ? "".concat(props.width, "px") : props.width;
        var height = isNumSize(props.height) ? "".concat(props.height, "px") : props.height;
        (0, vue_demi_1.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 = (0, vue_demi_1.defineComponent)({
    name: 'IconWidget.Provider',
    inheritAttrs: false,
    props: { tooltip: Boolean },
    setup: function (props, _a) {
        var slots = _a.slots;
        (0, vue_demi_1.provide)(IconSymbol, (0, vue_demi_1.computed)(function () { return props; }));
        return function () { var _a; return (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots); };
    },
});
exports.IconWidget = (0, __builtins__1.composeExport)(IconWidgetInner, {
    ShadowSVG: ShadowSVG,
    Provider: Provider,
});
//# sourceMappingURL=index.jsx.map