/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __assign, __read } from 'tslib';
import React__default, { useMemo, createElement, useEffect } from 'react';
import cx from 'classnames';
import SvgClose from '../icons/close.svg.js';
export { default as CloseIcon } from '../icons/close.svg.js';
import SvgCloseSmall from '../icons/close-small.svg.js';
import SvgStatusClose from '../icons/status-close.svg.js';
import SvgUndo from '../icons/undo.svg.js';
export { default as UnDoIcon } from '../icons/undo.svg.js';
import SvgUndoNormal from '../icons/undo-normal.svg.js';
import SvgRedo from '../icons/redo.svg.js';
export { default as ReDoIcon } from '../icons/redo.svg.js';
import SvgEnter from '../icons/enter.svg.js';
export { default as EnterIcon } from '../icons/enter.svg.js';
import SvgVolume from '../icons/volume.svg.js';
export { default as VolumeIcon } from '../icons/volume.svg.js';
import SvgMute from '../icons/mute.svg.js';
export { default as MuteIcon } from '../icons/mute.svg.js';
import SvgPlay from '../icons/play.svg.js';
export { default as PlayIcon } from '../icons/play.svg.js';
import SvgPause from '../icons/pause.svg.js';
export { default as PauseIcon } from '../icons/pause.svg.js';
import SvgLeftArrow from '../icons/left-arrow.svg.js';
export { default as LeftArrowIcon } from '../icons/left-arrow.svg.js';
import SvgRightArrow from '../icons/right-arrow.svg.js';
export { default as RightArrowIcon } from '../icons/right-arrow.svg.js';
import SvgArrowDoubleLeft from '../icons/arrow-double-left.svg.js';
import SvgArrowDoubleRight from '../icons/arrow-double-right.svg.js';
import SvgCheck from '../icons/check.svg.js';
export { default as CheckIcon } from '../icons/check.svg.js';
import SvgPlus from '../icons/plus.svg.js';
export { default as PlusIcon } from '../icons/plus.svg.js';
import SvgSubPlus from '../icons/sub-plus.svg.js';
import SvgMinus from '../icons/minus.svg.js';
export { default as MinusIcon } from '../icons/minus.svg.js';
import SvgPencil from '../icons/pencil.svg.js';
export { default as PencilIcon } from '../icons/pencil.svg.js';
import SvgView from '../icons/view.svg.js';
import SvgRemove from '../icons/remove.svg.js';
import SvgRetry from '../icons/retry.svg.js';
import SvgUpload from '../icons/upload.svg.js';
import SvgDownload from '../icons/download.svg.js';
import SvgFile from '../icons/file.svg.js';
import SvgSuccess from '../icons/success.svg.js';
import SvgFail from '../icons/fail.svg.js';
import SvgSearch from '../icons/search.svg.js';
import SvgBack from '../icons/back.svg.js';
import SvgMove from '../icons/move.svg.js';
import SvgInfo from '../icons/info.svg.js';
import SvgLocation from '../icons/location.svg.js';
import SvgDragBar from '../icons/drag-bar.svg.js';
import SvgReload from '../icons/reload.svg.js';
export { default as ReloadIcon } from '../icons/reload.svg.js';
import SvgExchange from '../icons/exchange.svg.js';
import SvgColumns from '../icons/columns.svg.js';
import SvgCalendar from '../icons/calendar.svg.js';
import SvgClock from '../icons/clock.svg.js';
import SvgTreeDown from '../icons/tree-down.svg.js';
import SvgCloudUpload from '../icons/cloud-upload.svg.js';
import SvgImage from '../icons/image.svg.js';
import SvgRefresh from '../icons/refresh.svg.js';
import SvgDrag from '../icons/drag.svg.js';
import SvgEdit from '../icons/edit.svg.js';
import SvgDeskEmpty from '../icons/desk-empty.svg.js';
import SvgFullscreen from '../icons/fullscreen.svg.js';
import SvgUnfullscreen from '../icons/unfullscreen.svg.js';
import SvgCopy from '../icons/copy.svg.js';
import SvgFilter from '../icons/filter.svg.js';
import SvgCaret from '../icons/caret.svg.js';
import SvgRightArrowBold from '../icons/right-arrow-bold.svg.js';
import SvgDownArrowBold from '../icons/down-arrow-bold.svg.js';
export { default as DownArrowBoldIcon } from '../icons/down-arrow-bold.svg.js';
import SvgColumnFilter from '../icons/column-filter.svg.js';
import SvgZoomIn from '../icons/zoom-in.svg.js';
import SvgZoomOut from '../icons/zoom-out.svg.js';
import SvgQuestion from '../icons/question.svg.js';
import SvgQuestion2 from '../icons/question2.svg.js';
import SvgQuestionMark from '../icons/question-mark.svg.js';
import SvgWindowRestore from '../icons/window-restore.svg.js';
import SvgInfoCircle from '../icons/info-circle.svg.js';
import SvgWarning from '../icons/warning.svg.js';
import SvgWarningMark from '../icons/warning-mark.svg.js';
import SvgSchedule from '../icons/schedule.svg.js';
import SvgHome from '../icons/home.svg.js';
import SvgFolder from '../icons/folder.svg.js';
import SvgSortDefault from '../icons/sort-default.svg.js';
import SvgSortAsc from '../icons/sort-asc.svg.js';
import SvgSortDesc from '../icons/sort-desc.svg.js';
import SvgSetting from '../icons/setting.svg.js';
import SvgPlusCicle from '../icons/plus-cicle.svg.js';
import SvgPlusFine from '../icons/plus-fine.svg.js';
import SvgEllipsisV from '../icons/ellipsis-v.svg.js';
import SvgExpandAlt from '../icons/expand-alt.svg.js';
import SvgCompressAlt from '../icons/compress-alt.svg.js';
import SvgTransparent from '../icons/transparent.svg.js';
import SvgLoadingOutline from '../icons/loading-outline.svg.js';
import SvgStar from '../icons/star.svg.js';
import SvgAlertSuccess from '../icons/alert-success.svg.js';
import SvgAlertInfo from '../icons/alert-info.svg.js';
import SvgAlertWarning from '../icons/alert-warning.svg.js';
import SvgAlertDanger from '../icons/alert-danger.svg.js';
import SvgFunction from '../icons/function.svg.js';
export { default as FunctionIcon } from '../icons/function.svg.js';
import SvgInputClear from '../icons/input-clear.svg.js';
export { default as InputClearIcon } from '../icons/input-clear.svg.js';
import SvgSliderHandleIcon from '../icons/slider-handle-icon.svg.js';
import SvgTrash from '../icons/trash.svg.js';
import SvgMenu from '../icons/menu.svg.js';
export { default as MenuIcon } from '../icons/menu.svg.js';
import SvgUserRemove from '../icons/user-remove.svg.js';
export { default as UserRemove } from '../icons/user-remove.svg.js';
import SvgRole from '../icons/role.svg.js';
export { default as Role } from '../icons/role.svg.js';
import SvgDepartment from '../icons/department.svg.js';
export { default as Department } from '../icons/department.svg.js';
import SvgPost from '../icons/post.svg.js';
export { default as Post } from '../icons/post.svg.js';
import SvgDot from '../icons/dot.svg.js';
import SvgStepsFinsh from '../icons/steps-finsh.svg.js';
import SvgInvisible from '../icons/invisible.svg.js';
import SvgDate from '../icons/date.svg.js';
import SvgDown from '../icons/down.svg.js';
import SvgRightDoubleArrow from '../icons/right-double-arrow.svg.js';
export { default as RightDoubleArrowIcon } from '../icons/right-double-arrow.svg.js';
import SvgNewEdit from '../icons/new-edit.svg.js';
import SvgRotateLeft from '../icons/rotate-left.svg.js';
import SvgRotateRight from '../icons/rotate-right.svg.js';
import SvgScaleOrigin from '../icons/scale-origin.svg.js';
import SvgIf from '../icons/if.svg.js';
import SvgRotateScreen from '../icons/rotate-screen.svg.js';
import isObject from 'lodash/isObject';
import { getCustomVendor } from 'amis-core';

// 兼容原来的用法，后续不直接试用。
var closeIcon = React__default.createElement(SvgClose, null);
var unDoIcon = React__default.createElement(SvgUndo, null);
var reDoIcon = React__default.createElement(SvgRedo, null);
var enterIcon = React__default.createElement(SvgEnter, null);
var volumeIcon = React__default.createElement(SvgVolume, null);
var muteIcon = React__default.createElement(SvgMute, null);
var playIcon = React__default.createElement(SvgPlay, null);
var pauseIcon = React__default.createElement(SvgPause, null);
var leftArrowIcon = React__default.createElement(SvgLeftArrow, null);
var rightArrowIcon = React__default.createElement(SvgRightArrow, null);
var iconFactory = {};
function getIconNames() {
    return Object.keys(iconFactory);
}
function getIcon(key) {
    return iconFactory[key];
}
function hasIcon(iconName) {
    return !!getIcon(iconName);
}
function registerIcon(key, component) {
    iconFactory[key] = component;
}
registerIcon('close', SvgClose);
registerIcon('close-small', SvgCloseSmall);
registerIcon('status-close', SvgStatusClose);
registerIcon('undo', SvgUndo);
registerIcon('undo-normal', SvgUndoNormal);
registerIcon('full-screen', SvgFullscreen);
registerIcon('un-fullscreen', SvgUnfullscreen);
registerIcon('redo', SvgRedo);
registerIcon('enter', SvgEnter);
registerIcon('volume', SvgVolume);
registerIcon('mute', SvgMute);
registerIcon('play', SvgPlay);
registerIcon('pause', SvgPause);
registerIcon('left-arrow', SvgLeftArrow);
registerIcon('right-arrow', SvgRightArrow);
registerIcon('prev', SvgLeftArrow);
registerIcon('next', SvgRightArrow);
registerIcon('check', SvgCheck);
registerIcon('plus', SvgPlus);
registerIcon('sub-plus', SvgSubPlus);
registerIcon('add', SvgPlus);
registerIcon('minus', SvgMinus);
registerIcon('pencil', SvgPencil);
registerIcon('view', SvgView);
registerIcon('remove', SvgRemove);
registerIcon('retry', SvgRetry);
registerIcon('upload', SvgUpload);
registerIcon('download', SvgDownload);
registerIcon('file', SvgFile);
registerIcon('success', SvgSuccess);
registerIcon('fail', SvgFail);
registerIcon('warning', SvgWarning);
registerIcon('warning-mark', SvgWarningMark);
registerIcon('search', SvgSearch);
registerIcon('back', SvgBack);
registerIcon('move', SvgMove);
registerIcon('info', SvgInfo);
registerIcon('info-circle', SvgInfoCircle);
registerIcon('location', SvgLocation);
registerIcon('drag-bar', SvgDragBar);
registerIcon('reload', SvgReload);
registerIcon('exchange', SvgExchange);
registerIcon('columns', SvgColumns);
registerIcon('calendar', SvgCalendar);
registerIcon('clock', SvgClock);
registerIcon('copy', SvgCopy);
registerIcon('filter', SvgFilter);
registerIcon('column-filter', SvgColumnFilter);
registerIcon('caret', SvgCaret);
registerIcon('right-arrow-bold', SvgRightArrowBold);
registerIcon('down-arrow-bold', SvgDownArrowBold);
registerIcon('zoom-in', SvgZoomIn);
registerIcon('zoom-out', SvgZoomOut);
registerIcon('question', SvgQuestion);
registerIcon('question2', SvgQuestion2);
registerIcon('question-mark', SvgQuestionMark);
registerIcon('window-restore', SvgWindowRestore);
registerIcon('schedule', SvgSchedule);
registerIcon('home', SvgHome);
registerIcon('folder', SvgFolder);
registerIcon('sort-default', SvgSortDefault);
registerIcon('sort-asc', SvgSortAsc);
registerIcon('sort-desc', SvgSortDesc);
registerIcon('setting', SvgSetting);
registerIcon('plus-cicle', SvgPlusCicle);
registerIcon('ellipsis-v', SvgEllipsisV);
registerIcon('expand-alt', SvgExpandAlt);
registerIcon('compress-alt', SvgCompressAlt);
registerIcon('transparent', SvgTransparent);
registerIcon('loading-outline', SvgLoadingOutline);
registerIcon('star', SvgStar);
registerIcon('alert-success', SvgAlertSuccess);
registerIcon('alert-info', SvgAlertInfo);
registerIcon('alert-warning', SvgAlertWarning);
registerIcon('alert-danger', SvgAlertDanger);
registerIcon('alert-fail', SvgAlertDanger);
registerIcon('tree-down', SvgTreeDown);
registerIcon('function', SvgFunction);
registerIcon('input-clear', SvgInputClear);
registerIcon('slider-handle', SvgSliderHandleIcon);
registerIcon('cloud-upload', SvgCloudUpload);
registerIcon('image', SvgImage);
registerIcon('refresh', SvgRefresh);
registerIcon('trash', SvgTrash);
registerIcon('menu', SvgMenu);
registerIcon('user-remove', SvgUserRemove);
registerIcon('role', SvgRole);
registerIcon('department', SvgDepartment);
registerIcon('post', SvgPost);
registerIcon('dot', SvgDot);
registerIcon('drag', SvgDrag);
registerIcon('edit', SvgEdit);
registerIcon('desk-empty', SvgDeskEmpty);
registerIcon('invisible', SvgInvisible);
registerIcon('plus-fine', SvgPlusFine);
registerIcon('steps-finsh', SvgStepsFinsh);
registerIcon('date', SvgDate);
registerIcon('remove', SvgRemove);
registerIcon('invisible', SvgInvisible);
registerIcon('down', SvgDown);
registerIcon('right-double-arrow', SvgRightDoubleArrow);
registerIcon('arrow-double-left', SvgArrowDoubleLeft);
registerIcon('arrow-double-right', SvgArrowDoubleRight);
registerIcon('new-edit', SvgNewEdit);
registerIcon('rotate-left', SvgRotateLeft);
registerIcon('rotate-right', SvgRotateRight);
registerIcon('scale-origin', SvgScaleOrigin);
registerIcon('if', SvgIf);
registerIcon('rotate-screen', SvgRotateScreen);
function svgString2Dom(icon, _a, vendorFn) {
    var className = _a.className, classNameProp = _a.classNameProp, style = _a.style, cx = _a.cx, events = _a.events, extra = _a.extra;
    icon = icon.replace(/\n/g, ' ').replace(/\s+/g, ' ');
    if (vendorFn) {
        var _b = vendorFn(icon, __assign(__assign({}, extra), { width: style === null || style === void 0 ? void 0 : style.width, height: style === null || style === void 0 ? void 0 : style.height })), newIcon = _b.icon, newStyle = _b.style;
        icon = newIcon;
        style = __assign(__assign({}, (style || {})), newStyle);
    }
    var svgStr = /<svg .*?>(.*?)<\/svg>/.exec(icon);
    var viewBox = /viewBox="(.*?)"/.exec(icon);
    var svgHTML = createElement('svg', __assign(__assign({}, events), { className: cx('icon', className, classNameProp), style: style, dangerouslySetInnerHTML: { __html: svgStr ? svgStr[1] : '' }, viewBox: (viewBox === null || viewBox === void 0 ? void 0 : viewBox[1]) || '0 0 16 16' }));
    return svgHTML;
}
function LinkIcon(_a) {
    var icon = _a.icon, vendorFn = _a.vendorFn, _b = _a.options, className = _b.className, classNameProp = _b.classNameProp, style = _b.style, cx = _b.cx, classPrefix = _b.classPrefix, events = _b.events, extra = _b.extra;
    var _c = __read(React__default.useState(icon), 2), svgIcon = _c[0], setSvgIcon = _c[1];
    var _d = __read(React__default.useState('img'), 2), svgType = _d[0], setSvgType = _d[1];
    useEffect(function () {
        if (icon.endsWith('.svg') && vendorFn) {
            try {
                fetch(icon)
                    .then(function (res) { return res.text(); })
                    .then(function (svg) {
                    setSvgType('svg');
                    setSvgIcon(svg);
                })
                    .catch(function (e) {
                    console.warn(e);
                    setSvgType('img');
                    setSvgIcon(icon);
                });
            }
            catch (warn) {
                console.warn(warn);
                setSvgType('img');
                setSvgIcon(icon);
            }
        }
    }, [icon, vendorFn]);
    return svgType === 'img' ? (React__default.createElement("img", __assign({}, events, { className: cx("".concat(classPrefix, "Icon"), className, classNameProp), src: icon, style: style }))) : (svgString2Dom(svgIcon, {
        className: className,
        classNameProp: classNameProp,
        style: style,
        cx: cx,
        events: events,
        extra: extra
    }, vendorFn));
}
function Icon(_a) {
    var _b;
    var icon = _a.icon, className = _a.className, _c = _a.classPrefix, classPrefix = _c === void 0 ? '' : _c, classNameProp = _a.classNameProp, iconContent = _a.iconContent, vendor = _a.vendor, iconCx = _a.cx, onClick = _a.onClick, onMouseEnter = _a.onMouseEnter, onMouseLeave = _a.onMouseLeave, onMouseOver = _a.onMouseOver, onMouseOut = _a.onMouseOut, onMouseDown = _a.onMouseDown, onMouseUp = _a.onMouseUp, onMouseMove = _a.onMouseMove, onBlur = _a.onBlur, onFocus = _a.onFocus, onTouchStart = _a.onTouchStart, onTouchMove = _a.onTouchMove, onTouchEnd = _a.onTouchEnd, onTouchCancel = _a.onTouchCancel, style = _a.style, width = _a.width, height = _a.height, extra = _a.extra, testIdBuilder = _a.testIdBuilder;
    var cx$1 = iconCx || cx;
    var vendorFn = useMemo(function () { return getCustomVendor(vendor); }, [vendor]);
    // style = {
    //   ...(style || {}),
    //   width: width || style?.width,
    //   height: height || style?.height
    // };
    if (width !== undefined) {
        style = __assign(__assign({}, style), { width: width });
    }
    if (height !== undefined) {
        style = __assign(__assign({}, style), { height: height });
    }
    if (typeof jest !== 'undefined' && icon) {
        iconContent = '';
    }
    if (!icon && !iconContent) {
        return null;
    }
    // 支持的事件
    var events = {
        onClick: onClick,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        onMouseOver: onMouseOver,
        onMouseOut: onMouseOut,
        onMouseDown: onMouseDown,
        onMouseUp: onMouseUp,
        onMouseMove: onMouseMove,
        onBlur: onBlur,
        onFocus: onFocus,
        onTouchStart: onTouchStart,
        onTouchMove: onTouchMove,
        onTouchEnd: onTouchEnd,
        onTouchCancel: onTouchCancel
    };
    // 直接的icon dom
    if (React__default.isValidElement(icon)) {
        return React__default.cloneElement(icon, __assign(__assign(__assign({}, events), (icon.props || {})), { className: cx(cx$1(className, classNameProp), icon.props.className), style: style }));
    }
    if (iconContent) {
        // 从css变量中获取icon
        var refFn = function (dom) {
            if (dom) {
                var domStyle = getComputedStyle(dom);
                var svgStr = domStyle.getPropertyValue('content');
                var svg = /(<svg.*<\/svg>)/.exec(svgStr);
                if (svg) {
                    var svgHTML = svg[0].replace(/\\"/g, '"');
                    if (dom.svgHTMLClone !== svgHTML) {
                        dom.innerHTML = svgHTML;
                        // 存储svg，不直接用innerHTML是防止<circle />渲染后变成<circle></circle>的情况
                        dom.svgHTMLClone = svgHTML;
                        dom.style.display = '';
                    }
                }
            }
        };
        return (React__default.createElement("div", __assign({}, events, { className: cx$1(iconContent, className, classNameProp), ref: refFn, style: style }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId())));
    }
    // 获取注册的icon
    var Component = getIcon(icon);
    if (Component) {
        return (React__default.createElement(Component, __assign({}, events, { className: cx$1(className, "icon-".concat(icon), classNameProp), 
            // @ts-ignore
            icon: icon, style: style }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId())));
    }
    // 符合schema的icon
    if (isObject(icon) &&
        icon.type === 'icon' &&
        icon.icon) {
        icon = icon.icon;
    }
    // icon是引用svg的情况
    if (isObject(icon) &&
        typeof icon.id === 'string' &&
        icon.id.startsWith('svg-')) {
        var svg = icon;
        var id = "".concat(svg.id.replace(/^svg-/, ''));
        if (!document.getElementById(id)) {
            // 如果svg symbol不存在，则尝试将svg字符串赋值给icon，走svg字符串的逻辑
            icon = (_b = svg.svg) === null || _b === void 0 ? void 0 : _b.replace(/'/g, '');
        }
        else {
            return (React__default.createElement("svg", __assign({}, events, { className: cx$1('icon', 'icon-object', className, classNameProp), style: style }),
                React__default.createElement("use", { xlinkHref: '#' + id })));
        }
    }
    // 直接传入svg字符串
    if (typeof icon === 'string' && icon.startsWith('<svg')) {
        return svgString2Dom(icon, {
            className: className,
            classNameProp: classNameProp,
            style: style,
            cx: cx$1,
            events: events,
            extra: extra
        }, vendorFn);
    }
    // icon是链接
    var isURLIcon = typeof icon === 'string' && (icon === null || icon === void 0 ? void 0 : icon.indexOf('.')) !== -1;
    if (isURLIcon) {
        return (React__default.createElement(LinkIcon, { icon: icon, vendorFn: vendorFn, options: {
                className: className,
                classNameProp: classNameProp,
                style: style,
                cx: cx$1,
                classPrefix: classPrefix,
                events: events,
                extra: extra
            } }));
    }
    // icon是普通字符串
    var isIconfont = typeof icon === 'string';
    var iconPrefix = '';
    if (vendor === 'iconfont') {
        iconPrefix = "iconfont icon-".concat(icon);
    }
    else if (vendor === 'fa') {
        //默认是fontawesome v4，兼容之前配置
        iconPrefix = "".concat(vendor, " ").concat(vendor, "-").concat(icon);
    }
    else {
        // 如果vendor为空，则不设置前缀,这样可以支持fontawesome v5、fontawesome v6或者其他框架
        iconPrefix = icon;
    }
    if (isIconfont) {
        return (React__default.createElement("i", __assign({}, events, { className: cx$1(icon, className, classNameProp, iconPrefix), style: style })));
    }
    // 没有合适的图标
    return React__default.createElement("span", { className: "text-danger" },
        "\u6CA1\u6709 icon ",
        icon);
}

export { Icon, closeIcon, Icon as default, enterIcon, getIcon, getIconNames, hasIcon, leftArrowIcon, muteIcon, pauseIcon, playIcon, reDoIcon, registerIcon, rightArrowIcon, unDoIcon, volumeIcon };
