import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["size"];
import React from 'react';
import { forwardRefAndIconName } from "../../../lib/forwardRefAndName";
import { ALIASES_TO_SIZES, DEFAULT_ICON_ALIAS } from "../iconConstants";
var isAlias = function isAlias(size) {
  return typeof size === 'string' && Object.keys(ALIASES_TO_SIZES).includes(size);
};
var getAliasFromSize = function getAliasFromSize(size) {
  return Object.entries(ALIASES_TO_SIZES).sort(function (_ref, _ref2) {
    var a = _ref[1];
    var b = _ref2[1];
    return Math.abs(size - a) > Math.abs(size - b) ? 1 : -1;
  })[0][0];
};
export var iconSizer = function iconSizer(sizes, iconName) {
  return forwardRefAndIconName(iconName, function (_ref3, ref) {
    var _ref3$size = _ref3.size,
      size = _ref3$size === void 0 ? DEFAULT_ICON_ALIAS : _ref3$size,
      props = _objectWithoutPropertiesLoose(_ref3, _excluded);
    var alias = DEFAULT_ICON_ALIAS;
    if (size !== alias && isAlias(size)) {
      alias = size;
    } else if (typeof size === 'number') {
      alias = getAliasFromSize(size);
    }
    var Icon = sizes[alias]();
    return /*#__PURE__*/React.cloneElement(Icon, _extends({
      ref: ref
    }, props));
  });
};