/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, computed, reactive, watch, createVNode, mergeProps } from 'vue';
import throttle from 'lodash/throttle';
import props from '../../props.js';
import '../../utils/index.js';
import { Input } from '../../../input/index.js';
import { InputNumber } from '../../../input-number/index.js';
import { FORMAT_INPUT_CONFIG } from './config.js';
import { Color } from '../../../_common/js/color-picker/color.js';
import '../../../_common/js/color-picker/index.js';
import '../../../_common/js/color-picker/cmyk.js';
import '../../../_common/js/color-picker/draggable.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../../../_common/js/color-picker/gradient.js';
import 'lodash/isString';
import 'lodash/isNull';
import 'tinycolor2';
import '@babel/runtime/helpers/slicedToArray';
import '../../../utils/withInstall.js';
import '../../../input/input.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@ldesign/icons-vue-next';
import 'lodash/isUndefined';
import '../../../form/hooks.js';
import '../../../hooks/useConfig.js';
import '../../../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import '../../../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../../../_common/js/global-config/default-config.js';
import '../../../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../../../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../../../_chunks/dep-4580aabe.js';
import '../../../config-provider/type.js';
import '../../../hooks/useGlobalIcon.js';
import '../../../hooks/tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';
import '../../../input/props.js';
import '../../../input/useInput.js';
import '../../../form/const.js';
import '../../../hooks/index.js';
import '../../../hooks/icon.js';
import '../../../hooks/slot.js';
import '../../../hooks/useCommonClassName.js';
import '../../../hooks/useDefaultValue.js';
import '../../../hooks/useDestroyOnClose.js';
import '../../../hooks/useKeepAnimation.js';
import '../../../hooks/useRipple.js';
import '../../../utils/set-style.js';
import '../../../hooks/useVirtualScroll.js';
import '../../../hooks/useVModel.js';
import '../../../input/useLengthLimit.js';
import 'lodash/isNumber';
import '../../../_common/js/log/index.js';
import '../../../_common/js/log/log.js';
import '../../../_common/js/utils/helper.js';
import '../../../input/useInputEventHandler.js';
import '../../../input/useInputWidth.js';
import '../../../input/input-group.js';
import '../../../input/input-group-props.js';
import '../../../input/style';
import '../../../input/type.js';
import '../../../input-number/input-number.js';
import '../../../button/index.js';
import '../../../button/button.js';
import '../../../loading/index.js';
import '../../../loading/directive.js';
import '../../../loading/plugin.js';
import '../../../utils/dom.js';
import '../../../utils/easing.js';
import '../../../loading/loading.js';
import '../../../hooks/useTeleport.js';
import '../../../loading/props.js';
import '../../../loading/icon/gradient.js';
import '../../../_common/js/loading/circle-adapter.js';
import '../../../_common/js/utils/set-style.js';
import '../../../loading/style';
import '../../../loading/type.js';
import '../../../button/props.js';
import '../../../button/style';
import '../../../button/type.js';
import '../../../input-number/props.js';
import '../../../input-number/useInputNumber.js';
import '../../../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../../../_common/js/input-number/large-number.js';
import '../../../input-number/style';
import '../../../input-number/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var FormatInputs = defineComponent({
  name: "FormatInputs",
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, props), {}, {
    color: {
      type: Object
    },
    onInputChange: {
      type: Function,
      "default": function _default() {
        return function () {};
      }
    }
  }),
  setup: function setup(props2) {
    var inputConfigs = computed(function () {
      var configs = _toConsumableArray(FORMAT_INPUT_CONFIG[props2.format]);
      if (props2.enableAlpha) {
        configs.push({
          type: "inputNumber",
          key: "a",
          min: 0,
          max: 100,
          format: function format(value) {
            return "".concat(value, "%");
          },
          flex: 1.15
        });
      }
      return configs;
    });
    var modelValue = reactive({});
    var lastModelValue = reactive({});
    var getFormatColorMap = function getFormatColorMap(type) {
      var color = props2.color;
      if (type === "encode") {
        return {
          HSV: color.getHsva(),
          HSL: color.getHsla(),
          RGB: color.getRgba(),
          CMYK: color.getCmyk(),
          CSS: {
            css: color.css
          },
          HEX: {
            hex: color.hex
          }
        };
      }
      return {
        HSV: Color.object2color(modelValue, "HSV"),
        HSL: Color.object2color(modelValue, "HSL"),
        RGB: Color.object2color(modelValue, "RGB"),
        CMYK: Color.object2color(modelValue, "CMYK"),
        CSS: modelValue.css,
        HEX: modelValue.hex
      };
    };
    var updateModelValue = function updateModelValue() {
      var format = props2.format,
        color = props2.color;
      var values = getFormatColorMap("encode")[format];
      values.a = Math.round(color.alpha * 100);
      Object.keys(values).forEach(function (key) {
        modelValue[key] = values[key];
        lastModelValue[key] = values[key];
      });
    };
    updateModelValue();
    var throttleUpdate = throttle(updateModelValue, 100);
    watch(function () {
      var _props2$color = props2.color,
        saturation = _props2$color.saturation,
        hue = _props2$color.hue,
        value = _props2$color.value,
        alpha = _props2$color.alpha;
      return [saturation, hue, value, alpha, props2.format];
    }, throttleUpdate);
    var handleChange = function handleChange(key, v) {
      if (v === lastModelValue[key]) return;
      var value = getFormatColorMap("decode")[props2.format];
      props2.onInputChange(value, modelValue.a / 100, key, v);
    };
    return {
      modelValue: modelValue,
      inputConfigs: inputConfigs,
      handleChange: handleChange
    };
  },
  render: function render() {
    var _this = this;
    var inputProps = _objectSpread({}, this.inputProps || {});
    return createVNode("div", {
      "class": "input-group"
    }, [this.inputConfigs.map(function (config) {
      return createVNode("div", {
        "class": "input-group__item",
        "key": config.key,
        "style": {
          flex: config.flex || 1
        }
      }, [config.type === "input" ? createVNode(Input, mergeProps(inputProps, {
        "align": "center",
        "size": "small",
        "disabled": _this.disabled,
        "modelValue": _this.modelValue[config.key],
        "onUpdate:modelValue": function onUpdateModelValue($event) {
          return _this.modelValue[config.key] = $event;
        },
        "maxlength": _this.format === "HEX" ? 9 : void 0,
        "title": _this.modelValue[config.key],
        "onBlur": function onBlur(v) {
          return _this.handleChange(config.key, v);
        },
        "onEnter": function onEnter(v) {
          return _this.handleChange(config.key, v);
        }
      }), null) : createVNode(InputNumber, mergeProps(inputProps, {
        "align": "center",
        "size": "small",
        "disabled": _this.disabled,
        "modelValue": _this.modelValue[config.key],
        "onUpdate:modelValue": function onUpdateModelValue($event) {
          return _this.modelValue[config.key] = $event;
        },
        "title": _this.modelValue[config.key],
        "min": config.min,
        "max": config.max,
        "step": 1,
        "format": config.format,
        "theme": "normal",
        "onBlur": function onBlur(v) {
          return _this.handleChange(config.key, v);
        },
        "onEnter": function onEnter(v) {
          return _this.handleChange(config.key, v);
        }
      }), null)]);
    })]);
  }
});

export { FormatInputs as default };
//# sourceMappingURL=inputs.js.map
