/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

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 { Color } from '../utils/index.js';
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 { Button } from '../../button/index.js';
import baseProps from './base-props.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../utils/with-install.js';
import 'vue';
import '../../../config.js';
import '../../select/select.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import 'lodash/isObject';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-vmodel.js';
import 'lodash/kebabCase';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import '../../../hooks/render-lnode.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/transfer-dom.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../popup/index.js';
import '../../../utils/map-props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/style';
import '../../popup/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style';
import '../../input/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style';
import '../../tag/type.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 '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style';
import '../../tag-input/type.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style';
import '../../select-input/type.js';
import '../../common/fake-arrow.js';
import '../../../hooks/use-form-disabled.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 '../../checkbox/style';
import '../../checkbox/type.js';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../../select/utils.js';
import '../../select/props.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 '../../select/style';
import '../../select/type.js';
import '../../button/button.js';
import '../../button/props.js';
import '../../button/style';
import '../../button/type.js';

var SwatchesPanel = defineComponent({
  name: "SwatchesPanel",
  components: {
    LSelect: Select,
    LOption: Option,
    LButton: Button
  },
  props: {
    ...baseProps,
    colors: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: "\u7CFB\u7EDF\u8272\u5F69"
    },
    editable: {
      type: Boolean,
      default: false
    },
    handleSetColor: {
      type: Function,
      default: () => () => {}
    },
    handleAddColor: {
      type: Function,
      default: () => () => {}
    }
  },
  setup(props) {
    const baseClassName = useBaseClassName();
    const {
      t,
      global
    } = useConfig("colorPicker");
    const {
      global: confirmGlobal
    } = useConfig("popconfirm");
    const classPrefix = usePrefixClass();
    const {
      DeleteIcon: DeleteIcon$1,
      AddIcon: AddIcon$1
    } = useGlobalIcon({
      DeleteIcon: DeleteIcon,
      AddIcon: AddIcon
    });
    const {
      statusClassNames
    } = useCommonClassName();
    const visiblePopConfirm = ref(false);
    const colorInstance = computed(() => cloneDeep(props.color));
    const setVisiblePopConfirm = visible => {
      visiblePopConfirm.value = visible;
    };
    const handleClick = color => {
      props.handleSetColor(color);
    };
    const isEqualCurrentColor = color => Color.compare(color, colorInstance.value.css);
    const selectedColorIndex = computed(() => props.colors.findIndex(color => isEqualCurrentColor(color)));
    const handleRemoveColor = () => {
      var _props$handleChange;
      const colors = [...props.colors];
      const 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,
      global,
      confirmGlobal,
      classPrefix,
      DeleteIcon: DeleteIcon$1,
      AddIcon: AddIcon$1,
      baseClassName,
      statusClassNames,
      selectedColorIndex,
      visiblePopConfirm,
      setVisiblePopConfirm,
      handleClick,
      isEqualCurrentColor,
      handleRemoveColor
    };
  },
  render() {
    const h = arguments[0];
    const {
      baseClassName,
      statusClassNames,
      title,
      editable,
      DeleteIcon,
      AddIcon
    } = this;
    const swatchesClass = `${baseClassName}__swatches`;
    const renderActions = () => {
      if (!editable) return null;
      return h("div", {
        "class": `${swatchesClass}--actions`
      }, [h("span", {
        "attrs": {
          "role": "button"
        },
        "class": `${baseClassName}__icon`,
        "on": {
          "click": () => this.handleAddColor()
        }
      }, [h(AddIcon)]), this.colors.length > 0 ? h("span", {
        "attrs": {
          "role": "button"
        },
        "class": `${baseClassName}__icon`,
        "on": {
          "click": () => this.handleRemoveColor()
        }
      }, [h(DeleteIcon)]) : null]);
    };
    return h("div", {
      "class": swatchesClass
    }, [h("h3", {
      "class": `${swatchesClass}--title`
    }, [h("span", [title]), renderActions()]), h("ul", {
      "class": [`${swatchesClass}--items`, "narrow-scrollbar"]
    }, [this.colors.map(color => h("li", {
      "class": [`${swatchesClass}--item`, this.isEqualCurrentColor(color) && editable ? statusClassNames.active : ""],
      "key": color,
      "on": {
        "click": () => {
          if (this.disabled) return;
          this.handleClick(color);
        }
      }
    }, [h("div", {
      "class": [`${swatchesClass}--color`, `${baseClassName}--bg-alpha`]
    }, [h("span", {
      "class": `${swatchesClass}--inner`,
      "style": {
        background: color
      }
    })])]))])]);
  }
});

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