import { createVNode as _createVNode } from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

import { createFromIconfontCN } from '@ant-design/icons-vue';
import { DefaultStyleProps } from '../typings';
import { isImg, isUrl, withInstall } from '../utils';
import { isVNode } from 'vue';
import settings from '../settings';

/**
 * iconfont 脚本文件
 * 约定本地脚本路径 public/js/iconfont/<name>.js 或 src/assets/js/iconfont/<name>.js
 * @param url 本地存放直接写脚本名称即可
 */
var scriptUrl = function scriptUrl(url) {
  if (!url) return [];
  var result = [];
  var reg = /^(http|https|\/{1,2}).*/;

  if (typeof url === 'string') {
    result.push(url);
  } else {
    result.push.apply(result, _toConsumableArray(url));
  }

  return result.map(function (urlPath) {
    if (reg.test(urlPath)) {
      return urlPath;
    } else {
      return "/js/iconfont/".concat(urlPath.replace('/', ''), ".js");
    }
  });
};

var IconFont = createFromIconfontCN({
  // todo 如何从 ProLayout 拿到 iconfontUrl 配置地址
  scriptUrl: scriptUrl(settings.iconfontUrl)
});

var AppIcon = function AppIcon(props) {
  var icon = props.icon,
      className = props.className,
      style = props.style;

  if (isVNode(icon)) {
    return icon;
  } else if (typeof icon === 'string') {
    if (isUrl(icon) || isImg(icon)) {
      return _createVNode("img", {
        "src": icon,
        "alt": "icon",
        "class": className,
        "style": style
      }, null);
    }

    if (icon.startsWith('icon-')) {
      return _createVNode(IconFont, {
        "type": icon,
        "class": className,
        "style": _objectSpread({}, style)
      }, null);
    }
  }

  return _createVNode("span", {
    "class": 'anticon',
    "style": _objectSpread({}, style)
  }, null);
};

AppIcon.displayName = 'AppIcon';
AppIcon.props = _objectSpread({
  icon: [Object, String]
}, DefaultStyleProps);
export default withInstall(AppIcon);