var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

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 { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { useEffect } from 'react';
import classNames from 'classnames';
import ColorPanel from "./__private__/ColorPanel";
import { prefixCls, DEFAULT_COLOR, COLORPICKER_TYPE_IMG, DEFAULT_IMAGE_SRC, COLORPICKER_TYPE_GRADIENT, DEFAULT_ROTATE } from "./__private__/constant";
import { CreateFilter } from "../create-filter";
import orderBy from 'lodash/orderBy';

var ColorPicker = function ColorPicker(_a) {
  var _b, _c, _d;

  var _e = _a.color,
      color = _e === void 0 ? DEFAULT_COLOR : _e,
      onChange = _a.onChange,
      disabled = _a.disabled,
      getPopupContainer = _a.getPopupContainer,
      type = _a.type,
      imgSrc = _a.imgSrc,
      imgSize = _a.imgSize,
      _f = _a.stops,
      stops = _f === void 0 ? [] : _f,
      _g = _a.rotate,
      rotate = _g === void 0 ? DEFAULT_ROTATE : _g,
      _h = _a.pickColorFromMap,
      pickColorFromMap = _h === void 0 ? false : _h,
      toMeasureCanvas = _a.toMeasureCanvas,
      toGetColorCanvas = _a.toGetColorCanvas,
      _j = _a.clickAwayToClose,
      clickAwayToClose = _j === void 0 ? true : _j,
      handleCanvasColorPickerVisibleChange = _a.handleCanvasColorPickerVisibleChange,
      _k = _a.createFilterProps,
      createFilterProps = _k === void 0 ? {} : _k,
      props = __rest(_a, ["color", "onChange", "disabled", "getPopupContainer", "type", "imgSrc", "imgSize", "stops", "rotate", "pickColorFromMap", "toMeasureCanvas", "toGetColorCanvas", "clickAwayToClose", "handleCanvasColorPickerVisibleChange", "createFilterProps"]);

  var currentStops = stops.length ? stops : [{
    offset: 0.5,
    color: color
  }];
  var colorString = [];
  orderBy(currentStops, 'offset', 'asc').forEach(function (it) {
    return colorString.push(it.color + " " + it.offset * 100 + "%");
  });
  var string = colorString.join(',') || '';
  var gradientStyle = {
    // backgroundColor: color,
    backgroundImage: "linear-gradient(" + rotate + "deg, " + string + ")"
  };
  var style = {
    backgroundSize: imgSize,
    backgroundImage: "url(" + imgSrc + ")",
    backgroundRepeat: imgSrc === DEFAULT_IMAGE_SRC ? 'repeat' : 'no-repeat'
  };
  useEffect(function () {
    if (pickColorFromMap && toMeasureCanvas === undefined) {
      throw new Error("prop 'pickColorFromMap' should used with 'toMeasureCanvas' ");
    }
  }, [pickColorFromMap, toMeasureCanvas]);
  var renderToolTip = React.useMemo(function () {
    return _jsx(ColorPanel, __assign({}, props, {
      stops: stops,
      rotate: rotate,
      color: color,
      imgSrc: imgSrc,
      imgSize: imgSize,
      onChange: onChange,
      pickColorFromMap: pickColorFromMap,
      toMeasureCanvas: toMeasureCanvas,
      toGetColorCanvas: toGetColorCanvas,
      useShadowElementToPickColor: true,
      type: type,
      handleCanvasColorPickerVisibleChange: handleCanvasColorPickerVisibleChange
    }), void 0);
  }, [props, stops, rotate, color, imgSrc, imgSize, onChange, pickColorFromMap, toMeasureCanvas, toGetColorCanvas, type, handleCanvasColorPickerVisibleChange]);
  var block = type === COLORPICKER_TYPE_IMG ? _jsx("div", {
    className: classNames(prefixCls + "-img-block", (_b = {}, _b[prefixCls + "-block-disabled"] = disabled, _b)),
    style: style
  }, void 0) : type === COLORPICKER_TYPE_GRADIENT ? _jsx("div", {
    className: classNames(prefixCls + "-gradient-block", (_c = {}, _c[prefixCls + "-block-disabled"] = disabled, _c)),
    style: gradientStyle
  }, void 0) : _jsx("div", {
    style: {
      backgroundColor: color
    },
    className: classNames(prefixCls + "-color-block", (_d = {}, _d[prefixCls + "-block-disabled"] = disabled, _d))
  }, void 0);
  if (disabled) return block;
  return _jsx(CreateFilter, __assign({
    clickAway: clickAwayToClose,
    overlay: renderToolTip,
    getPopupContainer: getPopupContainer,
    className: prefixCls + "-create-filter",
    footerClassName: prefixCls + "-footer"
  }, createFilterProps, {
    children: block
  }), void 0);
};

export default ColorPicker;
var ColorPickerMemo = /*#__PURE__*/React.memo(ColorPicker);
ColorPickerMemo.displayName = 'ColorPicker';
export { ColorPickerMemo };

