/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, computed } from '@vue/composition-api';
import { DeleteIcon, AddIcon } from '@ldesign/icons-vue';
import { cloneDeep } from 'lodash';
import { Select, Option } from '../../select/index.js';
import { C as Color } from '../../../_chunks/dep-64c83d1d.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import { useBaseClassName } from '../hooks/index.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import { useGlobalIcon } from '../../../hooks/use-global-icon.js';
import useCommonClassName from '../../../hooks/use-common-classname.js';
import baseProps from './base-props.js';
import { Button } from '../../button/index.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../select/select.js';
import '../../../_chunks/dep-b13ef886.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import 'lodash/isString';
import 'lodash/isObject';
import '../../select/props.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import 'vue';
import '../../../_chunks/dep-33b15d9d.js';
import '../../../_chunks/dep-85e8b23b.js';
import '../../../_chunks/dep-fc383775.js';
import '../../../config.js';
import '../../../utils/dom.js';
import 'raf';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../_chunks/dep-a0f2090c.js';
import '../../../utils/mixins.js';
import '../../../utils/with-install.js';
import '../../loading/plugin.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../../utils/map-props.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../../select-input/hooks/use-multiple.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../common/fake-arrow.js';
import '../../select/option.js';
import '../../../utils/ripple.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../../hooks/use-lazy-load.js';
import '../../../_chunks/dep-5d06368e.js';
import '../../select/utils.js';
import '../../select/select-panel.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../select/hooks/use-select-options.js';
import '../../select/option-group.js';
import '../../button/button.js';
import '../../button/props.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 SwatchesPanel = defineComponent({
  name: "SwatchesPanel",
  components: {
    LSelect: Select,
    LOption: Option,
    LButton: Button
  },
  props: _objectSpread(_objectSpread({}, baseProps), {}, {
    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 = useBaseClassName();

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

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

    var classPrefix = usePrefixClass();

    var _useGlobalIcon = useGlobalIcon({
      DeleteIcon: DeleteIcon,
      AddIcon: AddIcon
    }),
        DeleteIcon$1 = _useGlobalIcon.DeleteIcon,
        AddIcon$1 = _useGlobalIcon.AddIcon;

    var _useCommonClassName = useCommonClassName(),
        statusClassNames = _useCommonClassName.statusClassNames;

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

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

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

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

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

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

      var colors = _toConsumableArray(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$1,
      AddIcon: AddIcon$1,
      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
        }
      })])]);
    })])]);
  }
});

export { SwatchesPanel as default };
//# sourceMappingURL=swatches.js.map
