import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode } from "vue";
import _extends from "@babel/runtime/helpers/esm/extends";
var __rest = this && this.__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
import { cloneVNode, computed, defineComponent, ref } from 'vue';
import omit from '../../_util/omit';
import { baseSliderProps } from '../../vc-color-picker';
import { initDefaultProps } from '../../_util/props-util';
import Slider from '../../slider';
import classNames from '../../_util/classNames';
import { useProvideUnstableSlider } from '../../ng-slider/context';
import { useProvideSliderInternal } from '../../slider/Context';
import { getGradientPercentColor } from '../util';
export const gradientColorSliderProps = () => _extends(_extends({}, omit(baseSliderProps(), ['value', 'onChange', 'onChangeComplete', 'type'])), {
  value: Array,
  onChange: Function,
  onChangeComplete: Function,
  range: Boolean,
  className: String,
  activeIndex: Number,
  onActive: Function,
  type: String,
  // Drag events
  onDragStart: Function,
  onDragChange: Function,
  // Key event
  onKeyDelete: Function
});
export const GradientColorSlider = defineComponent({
  name: 'GradientColorSlider',
  inheritAttrs: false,
  props: initDefaultProps(gradientColorSliderProps(), {
    range: false
  }),
  setup(props) {
    const linearCss = computed(() => {
      const colorsStr = props.colors.map(c => `${c.color} ${c.percent}%`).join(', ');
      return `linear-gradient(90deg, ${colorsStr})`;
    });
    const pointColor = computed(() => {
      if (!props.color || !props.type) {
        return null;
      }
      if (props.type === 'alpha') {
        return props.color.toRgbString();
      }
      return `hsl(${props.color.toHsb().h}, 100%, 50%)`;
    });
    // ======================= Context: Slider ========================
    const onInternalDragStart = props.onDragStart;
    const onInternalDragChange = props.onDragChange;
    useProvideUnstableSlider({
      onDragStart: onInternalDragStart,
      onDragChange: onInternalDragChange
    });
    // ======================= Context: Render ========================
    const handleRender = (ori, info) => {
      const {
        style,
        class: handleCls
      } = ori.props;
      // Point Color
      const mergedStyle = _extends({}, style);
      if (props.type === 'gradient') {
        mergedStyle.background = getGradientPercentColor(props.colors, info.value);
      }
      return cloneVNode(ori, {
        onFocus: () => {
          var _a;
          (_a = props.onActive) === null || _a === void 0 ? void 0 : _a.call(props, info.index);
        },
        style: mergedStyle,
        className: classNames(handleCls, {
          [`${props.prefixCls}-slider-handle-active`]: props.activeIndex === info.index
        }),
        onKeydown: e => {
          if ((e.key === 'Delete' || e.key === 'Backspace') && props.onKeyDelete) {
            props.onKeyDelete(info.index);
          }
        }
      });
    };
    useProvideSliderInternal({
      direction: ref('ltr'),
      handleRender
    });
    return () => {
      const {
          prefixCls,
          colors,
          type,
          color,
          range = false,
          className,
          activeIndex,
          onActive,
          onKeyDelete
        } = props,
        restProps = __rest(props, ["prefixCls", "colors", "type", "color", "range", "className", "activeIndex", "onActive", "onKeyDelete"]);
      const sliderProps = _extends(_extends({}, restProps), {
        track: false
      });
      return _createVNode(Slider, _objectSpread(_objectSpread({}, sliderProps), {}, {
        "class": classNames(className, `${prefixCls}-slider`),
        "tooltip": {
          open: false
        },
        "range": {
          editable: range,
          minCount: 2
        },
        "styles": {
          rail: {
            background: linearCss.value
          },
          handle: pointColor.value ? {
            background: pointColor.value
          } : {}
        },
        "classNames": {
          rail: `${prefixCls}-slider-rail`,
          handle: `${prefixCls}-slider-handle`
        }
      }), null);
    };
  }
});
const SingleColorSlider = defineComponent({
  name: 'SingleColorPicker',
  props: baseSliderProps(),
  setup(props) {
    const singleOnChange = v => props.onChange(v[0]);
    const singleOnChangeComplete = v => props.onChangeComplete(v[0]);
    return () => {
      return _createVNode(GradientColorSlider, _objectSpread(_objectSpread({}, props), {}, {
        "value": [props.value],
        "onChange": singleOnChange,
        "onChangeComplete": singleOnChangeComplete
      }), null);
    };
  }
});
export default SingleColorSlider;