"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ColorPickerMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var react_1 = require("react");

var classnames_1 = __importDefault(require("classnames"));

var ColorPanel_1 = __importDefault(require("./__private__/ColorPanel"));

var constant_1 = require("./__private__/constant");

var create_filter_1 = require("../create-filter");

var orderBy_1 = __importDefault(require("lodash/orderBy"));

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

  var _e = _a.color,
      color = _e === void 0 ? constant_1.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 ? constant_1.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,
      props = __rest(_a, ["color", "onChange", "disabled", "getPopupContainer", "type", "imgSrc", "imgSize", "stops", "rotate", "pickColorFromMap", "toMeasureCanvas", "toGetColorCanvas", "clickAwayToClose", "handleCanvasColorPickerVisibleChange"]);

  var currentStops = stops.length ? stops : [{
    offset: 0.5,
    color: color
  }];
  var colorString = [];
  orderBy_1["default"](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 === constant_1.DEFAULT_IMAGE_SRC ? 'repeat' : 'no-repeat'
  };
  react_1.useEffect(function () {
    if (pickColorFromMap && toMeasureCanvas === undefined) {
      throw new Error("prop 'pickColorFromMap' should used with 'toMeasureCanvas' ");
    }
  }, [pickColorFromMap, toMeasureCanvas]);
  var renderToolTip = React.useMemo(function () {
    return jsx_runtime_1.jsx(ColorPanel_1["default"], __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 === constant_1.COLORPICKER_TYPE_IMG ? jsx_runtime_1.jsx("div", {
    className: classnames_1["default"](constant_1.prefixCls + "-img-block", (_b = {}, _b[constant_1.prefixCls + "-block-disabled"] = disabled, _b)),
    style: style
  }, void 0) : type === constant_1.COLORPICKER_TYPE_GRADIENT ? jsx_runtime_1.jsx("div", {
    className: classnames_1["default"](constant_1.prefixCls + "-gradient-block", (_c = {}, _c[constant_1.prefixCls + "-block-disabled"] = disabled, _c)),
    style: gradientStyle
  }, void 0) : jsx_runtime_1.jsx("div", {
    style: {
      backgroundColor: color
    },
    className: classnames_1["default"](constant_1.prefixCls + "-color-block", (_d = {}, _d[constant_1.prefixCls + "-block-disabled"] = disabled, _d))
  }, void 0);
  if (disabled) return block;
  return jsx_runtime_1.jsx(create_filter_1.CreateFilter, __assign({
    clickAway: clickAwayToClose,
    overlay: renderToolTip,
    getPopupContainer: getPopupContainer,
    className: constant_1.prefixCls + "-create-filter",
    footerClassName: constant_1.prefixCls + "-footer"
  }, {
    children: block
  }), void 0);
};

exports["default"] = ColorPicker;
var ColorPickerMemo = React.memo(ColorPicker);
exports.ColorPickerMemo = ColorPickerMemo;
ColorPickerMemo.displayName = 'ColorPicker';

