"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _numberPicker = _interopRequireDefault(require("@alifd/next/lib/number-picker"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _utils = require("../../utils");
require("./index.less");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var _default = exports["default"] = function _default(props) {
  var styleData = props.styleData,
    styleKey = props.styleKey,
    unit = props.unit,
    onStyleChange = props.onStyleChange,
    min = props.min,
    max = props.max,
    _props$style = props.style,
    style = _props$style === void 0 ? {} : _props$style,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    placeholderScale = props.placeholderScale,
    onChangeFunction = props.onChangeFunction,
    multiProp = props.multiProp,
    defaultPlaceholder = props.defaultPlaceholder;
  var _useState = (0, _react.useState)(defaultPlaceholder),
    placeholder = _useState[0],
    setPlaceholder = _useState[1];
  var onNumberChange = function onNumberChange(styleKey, value, unit) {
    onStyleChange([{
      styleKey: styleKey,
      value: unit ? (0, _utils.addUnit)(value, unit) : String(value)
    }]);
  };
  var initData = function initData(props) {
    var field = props.field,
      styleKey = props.styleKey,
      useComputedStyle = props.useComputedStyle;
    if (useComputedStyle) {
      var _placeholder = (0, _utils.getPlaceholderPropertyValue)(field, styleKey);
      if (_placeholder && !isNaN(_placeholder)) {
        setPlaceholder(_placeholder * (1 / placeholderScale));
      } else {
        setPlaceholder('auto');
      }
    }
  };
  (0, _react.useEffect)(function () {
    initData(props);
  }, []);
  var value = unit ? (0, _utils.removeUnit)(styleData[styleKey]) : styleData[styleKey];
  var curUnit = unit ? (0, _utils.getUnit)(styleData[styleKey]) || 'px' : '';
  // 不加multiprop一样，加了单独处理
  if (typeof multiProp === 'number') {
    var _unifyStyle, _unifyStyle$split;
    value = (_unifyStyle = (0, _utils.unifyStyle)(styleData[styleKey])) === null || _unifyStyle === void 0 ? void 0 : (_unifyStyle$split = _unifyStyle.split(' ')) === null || _unifyStyle$split === void 0 ? void 0 : _unifyStyle$split[multiProp];
    if (value === null || value === undefined || value === 'auto') {
      value = null;
      curUnit = 'px';
    } else {
      curUnit = unit ? (0, _utils.getUnit)(value) || 'px' : '';
      value = unit ? (0, _utils.removeUnit)(value) : value;
    }
  }
  if (isNaN(value)) {
    value = 0;
  }
  var getInnerAfter = (0, _react.useMemo)(function () {
    if (typeof unit === 'string') {
      return unit;
    }
    if (!unit) {
      return '';
    }
    var options = unit === null || unit === void 0 ? void 0 : unit.map(function (item) {
      return {
        value: item,
        label: item
      };
    });
    return /*#__PURE__*/React.createElement(_select["default"], {
      defaultValue: "px",
      style: {
        width: '24px'
      },
      value: curUnit || 'px',
      autoWidth: false,
      hasBorder: false,
      hasArrow: false,
      onChange: function onChange(val) {
        return onChangeFunction ? onChangeFunction(styleKey, value, val) : onNumberChange(styleKey, value, val);
      },
      dataSource: options
    });
  }, [unit]);
  var originValue = styleData[styleKey];
  if ((0, _utils.isCssVarBind)(originValue)) {
    return /*#__PURE__*/React.createElement("div", {
      className: "ext-css-variable-ghost",
      title: originValue
    }, originValue);
  }
  return /*#__PURE__*/React.createElement(_numberPicker["default"], {
    style: style,
    className: className,
    value: value,
    min: (0, _utils.isEmptyValue)(min) ? Number.MIN_SAFE_INTEGER : min,
    max: (0, _utils.isEmptyValue)(max) ? Number.MAX_SAFE_INTEGER : max,
    onChange: function onChange(val) {
      return onChangeFunction ? onChangeFunction(styleKey, val, curUnit) : onNumberChange(styleKey, val, curUnit);
    },
    innerAfter: getInnerAfter,
    placeholder: placeholder
  });
};