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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var throttle = require('lodash/throttle');
var colorPicker_props = require('../../props.js');
var input_index = require('../../../input/index.js');
var inputNumber_index = require('../../../input-number/index.js');
var colorPicker_panel_format_config = require('./config.js');
var _common_js_colorPicker_color = require('../../../_common/js/color-picker/color.js');
require('../../../utils/withInstall.js');
require('../../../input/input.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@ldesign/icons-vue-next');
require('lodash/isUndefined');
require('../../../form/hooks.js');
require('../../../hooks/useConfig.js');
require('../../../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../../../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../../../_common/js/global-config/default-config.js');
require('../../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../../_chunks/dep-7f0559c3.js');
require('dayjs');
require('../../../hooks/useGlobalIcon.js');
require('../../../hooks/tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../../../input/props.js');
require('../../../input/useInput.js');
require('@babel/runtime/helpers/slicedToArray');
require('../../../form/const.js');
require('../../../hooks/useVModel.js');
require('../../../input/useLengthLimit.js');
require('lodash/isNumber');
require('../../../_common/js/log/log.js');
require('../../../_common/js/utils/helper.js');
require('lodash/isNull');
require('../../../input/useInputEventHandler.js');
require('../../../input/useInputWidth.js');
require('../../../input/input-group.js');
require('../../../input/input-group-props.js');
require('../../../input-number/input-number.js');
require('../../../button/index.js');
require('../../../button/button.js');
require('../../../loading/index.js');
require('../../../loading/directive.js');
require('../../../loading/plugin.js');
require('../../../utils/dom.js');
require('../../../utils/easing.js');
require('../../../loading/loading.js');
require('../../../hooks/useTeleport.js');
require('../../../loading/props.js');
require('../../../loading/icon/gradient.js');
require('../../../_common/js/loading/circle-adapter.js');
require('../../../_common/js/utils/set-style.js');
require('../../../hooks/useRipple.js');
require('../../../utils/set-style.js');
require('../../../hooks/useKeepAnimation.js');
require('../../../button/props.js');
require('../../../input-number/props.js');
require('../../../input-number/useInputNumber.js');
require('../../../hooks/useCommonClassName.js');
require('../../../_common/js/input-number/number.js');
require('lodash/isNil');
require('../../../_common/js/input-number/large-number.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('tinycolor2');
require('../../../_common/js/color-picker/cmyk.js');
require('../../../_common/js/color-picker/gradient.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);

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__default["default"](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 = vue.defineComponent({
  name: "FormatInputs",
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, colorPicker_props["default"]), {}, {
    color: {
      type: Object
    },
    onInputChange: {
      type: Function,
      "default": function _default() {
        return function () {};
      }
    }
  }),
  setup: function setup(props2) {
    var inputConfigs = vue.computed(function () {
      var configs = _toConsumableArray__default["default"](colorPicker_panel_format_config.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 = vue.reactive({});
    var lastModelValue = vue.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: _common_js_colorPicker_color.Color.object2color(modelValue, "HSV"),
        HSL: _common_js_colorPicker_color.Color.object2color(modelValue, "HSL"),
        RGB: _common_js_colorPicker_color.Color.object2color(modelValue, "RGB"),
        CMYK: _common_js_colorPicker_color.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__default["default"](updateModelValue, 100);
    vue.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 vue.createVNode("div", {
      "class": "input-group"
    }, [this.inputConfigs.map(function (config) {
      return vue.createVNode("div", {
        "class": "input-group__item",
        "key": config.key,
        "style": {
          flex: config.flex || 1
        }
      }, [config.type === "input" ? vue.createVNode(input_index.Input, vue.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) : vue.createVNode(inputNumber_index.InputNumber, vue.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)]);
    })]);
  }
});

exports["default"] = FormatInputs;
//# sourceMappingURL=inputs.js.map
