/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @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 VueCompositionAPI = require('@vue/composition-api');
var iconsVue = require('@ldesign/icons-vue');
var lodash = require('lodash');
var components_select_index = require('../../select/index.js');
var color = require('../../../chunks/color.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
var components_colorPicker_hooks_index = require('../hooks/index.js');
var hooks_useConfig = require('../../../hooks/use-config.js');
var hooks_useGlobalIcon = require('../../../hooks/use-global-icon.js');
var hooks_useCommonClassname = require('../../../hooks/use-common-classname.js');
var components_colorPicker_panel_baseProps = require('./base-props.js');
var components_button_index = require('../../button/index.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../select/select.js');
require('../../../chunks/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('lodash/isArray');
require('../../../hooks/use-default-value.js');
require('../../../hooks/use-vmodel.js');
require('../../../hooks/lnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../hooks/render-lnode.js');
require('lodash/isString');
require('lodash/isObject');
require('../../select/props.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../loading/icon/gradient.js');
require('vue');
require('../../../chunks/defineProperty-28395935.js');
require('../../../chunks/set-style.js');
require('../../../chunks/helper2.js');
require('../../../config.js');
require('../../../utils/dom.js');
require('raf');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('@babel/runtime/helpers/typeof');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../../utils/with-install.js');
require('../../loading/plugin.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../../utils/helper.js');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../../utils/map-props.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../../utils/event.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../select-input/props.js');
require('../../select-input/hooks/use-single.js');
require('lodash/pick');
require('../../select-input/hooks/use-multiple.js');
require('../../select-input/hooks/use-overlay-innerstyle.js');
require('../../common/fake-arrow.js');
require('../../select/option.js');
require('../../../utils/ripple.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../../hooks/use-lazy-load.js');
require('../../../chunks/observe.js');
require('../../select/utils.js');
require('../../select/select-panel.js');
require('../../../hooks/use-virtual-scroll.js');
require('../../select/hooks/use-select-options.js');
require('../../select/option-group.js');
require('../../button/button.js');
require('../../button/props.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);

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 SwatchesPanel = VueCompositionAPI.defineComponent({
  name: "SwatchesPanel",
  components: {
    LSelect: components_select_index.Select,
    LOption: components_select_index.Option,
    LButton: components_button_index.Button
  },
  props: _objectSpread(_objectSpread({}, components_colorPicker_panel_baseProps["default"]), {}, {
    colors: {
      type: Array,
      "default": function _default() {
        return [];
      }
    },
    title: {
      type: String,
      "default": "\u7CFB\u7EDF\u8272\u5F69"
    },
    editable: {
      type: Boolean,
      "default": false
    },
    handleSetColor: {
      type: Function,
      "default": function _default() {
        return function () {};
      }
    },
    handleAddColor: {
      type: Function,
      "default": function _default() {
        return function () {};
      }
    }
  }),
  setup: function setup(props) {
    var baseClassName = components_colorPicker_hooks_index.useBaseClassName();

    var _useConfig = components_configProvider_useConfig.useConfig("colorPicker"),
        t = _useConfig.t,
        global = _useConfig.global;

    var _useConfig2 = components_configProvider_useConfig.useConfig("popconfirm"),
        confirmGlobal = _useConfig2.global;

    var classPrefix = hooks_useConfig.usePrefixClass();

    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
      DeleteIcon: iconsVue.DeleteIcon,
      AddIcon: iconsVue.AddIcon
    }),
        DeleteIcon = _useGlobalIcon.DeleteIcon,
        AddIcon = _useGlobalIcon.AddIcon;

    var _useCommonClassName = hooks_useCommonClassname["default"](),
        statusClassNames = _useCommonClassName.statusClassNames;

    var visiblePopConfirm = VueCompositionAPI.ref(false);
    var colorInstance = VueCompositionAPI.computed(function () {
      return lodash.cloneDeep(props.color);
    });

    var setVisiblePopConfirm = function setVisiblePopConfirm(visible) {
      visiblePopConfirm.value = visible;
    };

    var handleClick = function handleClick(color) {
      props.handleSetColor(color);
    };

    var isEqualCurrentColor = function isEqualCurrentColor(color$1) {
      return color.Color.compare(color$1, colorInstance.value.css);
    };

    var selectedColorIndex = VueCompositionAPI.computed(function () {
      return props.colors.findIndex(function (color) {
        return isEqualCurrentColor(color);
      });
    });

    var handleRemoveColor = function handleRemoveColor() {
      var _props$handleChange;

      var colors = _toConsumableArray__default["default"](props.colors);

      var selectedIndex = selectedColorIndex.value;

      if (selectedIndex > -1) {
        colors.splice(selectedIndex, 1);
      } else {
        colors.length = 0;
      }

      (_props$handleChange = props.handleChange) === null || _props$handleChange === void 0 ? void 0 : _props$handleChange.call(props, colors);
      setVisiblePopConfirm(false);
    };

    return {
      t: t,
      global: global,
      confirmGlobal: confirmGlobal,
      classPrefix: classPrefix,
      DeleteIcon: DeleteIcon,
      AddIcon: AddIcon,
      baseClassName: baseClassName,
      statusClassNames: statusClassNames,
      selectedColorIndex: selectedColorIndex,
      visiblePopConfirm: visiblePopConfirm,
      setVisiblePopConfirm: setVisiblePopConfirm,
      handleClick: handleClick,
      isEqualCurrentColor: isEqualCurrentColor,
      handleRemoveColor: handleRemoveColor
    };
  },
  render: function render() {
    var _this = this;

    var h = arguments[0];
    var baseClassName = this.baseClassName,
        statusClassNames = this.statusClassNames,
        title = this.title,
        editable = this.editable,
        DeleteIcon = this.DeleteIcon,
        AddIcon = this.AddIcon;
    var swatchesClass = "".concat(baseClassName, "__swatches");

    var renderActions = function renderActions() {
      if (!editable) {
        return null;
      }

      return h("div", {
        "class": "".concat(swatchesClass, "--actions")
      }, [h("span", {
        "attrs": {
          "role": "button"
        },
        "class": "".concat(baseClassName, "__icon"),
        "on": {
          "click": function click() {
            return _this.handleAddColor();
          }
        }
      }, [h(AddIcon)]), _this.colors.length > 0 ? h("span", {
        "attrs": {
          "role": "button"
        },
        "class": "".concat(baseClassName, "__icon"),
        "on": {
          "click": function click() {
            return _this.handleRemoveColor();
          }
        }
      }, [h(DeleteIcon)]) : null]);
    };

    return h("div", {
      "class": swatchesClass
    }, [h("h3", {
      "class": "".concat(swatchesClass, "--title")
    }, [h("span", [title]), renderActions()]), h("ul", {
      "class": ["".concat(swatchesClass, "--items"), "narrow-scrollbar"]
    }, [this.colors.map(function (color) {
      return h("li", {
        "class": ["".concat(swatchesClass, "--item"), _this.isEqualCurrentColor(color) && editable ? statusClassNames.active : ""],
        "key": color,
        "on": {
          "click": function click() {
            if (_this.disabled) {
              return;
            }

            _this.handleClick(color);
          }
        }
      }, [h("div", {
        "class": ["".concat(swatchesClass, "--color"), "".concat(baseClassName, "--bg-alpha")]
      }, [h("span", {
        "class": "".concat(swatchesClass, "--inner"),
        "style": {
          background: color
        }
      })])]);
    })])]);
  }
});

exports["default"] = SwatchesPanel;
//# sourceMappingURL=swatches.js.map
