import { defineComponent, inject, ref, reactive, computed, onMounted, provide, resolveComponent, openBlock, createElementBlock, normalizeClass, createCommentVNode, createElementVNode, normalizeStyle, withDirectives, createVNode, withCtx, vShow, createBlock } from 'vue';
import { PsIcon } from '../../icon/index.mjs';
import '../../../directives/index.mjs';
import '../../../tokens/index.mjs';
import '../../../hooks/index.mjs';
import '../../../constants/index.mjs';
import '../../../utils/index.mjs';
import { Close, ArrowDown } from '@element-plus/icons-vue';
import Color from './color.mjs';
import { OPTIONS_KEY } from './useOption.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import ClickOutside from '../../../directives/click-outside/index.mjs';
import { isValidComponentSize } from '../../../utils/vue/validator.mjs';
import { UPDATE_MODEL_EVENT } from '../../../constants/event.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { formContextKey } from '../../../tokens/form.mjs';
import { useSize } from '../../../hooks/use-common-props/index.mjs';

const _sfc_main = defineComponent({
  name: "PsColorPicker",
  components: {
    PsIcon,
    Close,
    ArrowDown
  },
  directives: {
    ClickOutside
  },
  props: {
    modelValue: String,
    showAlpha: Boolean,
    colorFormat: String,
    disabled: Boolean,
    size: {
      type: String,
      validator: isValidComponentSize
    },
    popperClass: String,
    predefine: Array
  },
  emits: ["change", "active-change", UPDATE_MODEL_EVENT],
  setup(props, { emit }) {
    const { t } = useLocale();
    const ns = useNamespace("color");
    const elForm = inject(formContextKey, {});
    const hue = ref(null);
    const svPanel = ref(null);
    const alpha = ref(null);
    const popper = ref(null);
    const color = reactive(new Color({
      enableAlpha: props.showAlpha,
      format: props.colorFormat,
      value: props.modelValue
    }));
    const showPicker = ref(false);
    const showPanelColor = ref(false);
    const customInput = ref("");
    const displayedColor = computed(() => {
      if (!props.modelValue && !showPanelColor.value) {
        return "transparent";
      }
      return displayedRgb(color, props.showAlpha);
    });
    const colorSize = useSize();
    const colorDisabled = computed(() => {
      return !!(props.disabled || elForm.disabled);
    });
    const currentColor = computed(() => {
      return !props.modelValue && !showPanelColor.value ? "" : color.value;
    });
    function displayedRgb(color2, showAlpha) {
      if (!(color2 instanceof Color)) {
        throw new TypeError("color should be instance of _color Class");
      }
      const { r, g, b } = color2.toRgb();
      return showAlpha ? `rgba(${r}, ${g}, ${b}, ${color2.get("alpha") / 100})` : `rgb(${r}, ${g}, ${b})`;
    }
    onMounted(() => {
      if (props.modelValue) {
        customInput.value = currentColor.value;
      }
    });
    provide(OPTIONS_KEY, {
      currentColor
    });
    return {
      color,
      colorDisabled,
      colorSize,
      displayedColor,
      showPanelColor,
      showPicker,
      customInput,
      t,
      ns,
      hue,
      svPanel,
      alpha,
      popper
    };
  }
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  const _component_arrow_down = resolveComponent("arrow-down");
  const _component_ps_icon = resolveComponent("ps-icon");
  const _component_close = resolveComponent("close");
  return openBlock(), createElementBlock("div", {
    class: normalizeClass([
      _ctx.ns.b("picker"),
      _ctx.ns.is("disabled", _ctx.colorDisabled),
      _ctx.ns.bm("picker", _ctx.colorSize)
    ])
  }, [
    _ctx.colorDisabled ? (openBlock(), createElementBlock("div", {
      key: 0,
      class: normalizeClass(_ctx.ns.be("picker", "mask"))
    }, null, 2)) : createCommentVNode("v-if", true),
    createElementVNode("div", {
      class: normalizeClass(_ctx.ns.be("picker", "trigger"))
    }, [
      createElementVNode("span", {
        class: normalizeClass([_ctx.ns.be("picker", "color"), _ctx.ns.is("alpha", _ctx.showAlpha)])
      }, [
        createElementVNode("span", {
          class: normalizeClass(_ctx.ns.be("picker", "color-inner")),
          style: normalizeStyle({
            backgroundColor: _ctx.displayedColor
          })
        }, [
          withDirectives(createVNode(_component_ps_icon, {
            class: normalizeClass([_ctx.ns.be("picker", "icon"), _ctx.ns.is("icon-arrow-down")])
          }, {
            default: withCtx(() => [
              createVNode(_component_arrow_down)
            ]),
            _: 1
          }, 8, ["class"]), [
            [vShow, _ctx.modelValue || _ctx.showPanelColor]
          ]),
          !_ctx.modelValue && !_ctx.showPanelColor ? (openBlock(), createBlock(_component_ps_icon, {
            key: 0,
            class: normalizeClass([_ctx.ns.be("picker", "empty"), _ctx.ns.is("icon-close")])
          }, {
            default: withCtx(() => [
              createVNode(_component_close)
            ]),
            _: 1
          }, 8, ["class"])) : createCommentVNode("v-if", true)
        ], 6)
      ], 2)
    ], 2)
  ], 2);
}
var ColorPicker = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/Users/meng/workspace/element-ps/packages/components/color-picker/src/index.vue"]]);

export { ColorPicker as default };
//# sourceMappingURL=index.mjs.map
