"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.generateColor = exports.defaultColor = exports.calculateColor = exports.calcOffset = exports.ColorPickerPrefixCls = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _color = require("./color");
const ColorPickerPrefixCls = exports.ColorPickerPrefixCls = 'vc-color-picker';
const generateColor = color => {
  if (color instanceof _color.Color) {
    return color;
  }
  return new _color.Color(color);
};
exports.generateColor = generateColor;
const defaultColor = exports.defaultColor = generateColor('#1677ff');
const calculateColor = props => {
  const {
    offset,
    targetRef,
    containerRef,
    color,
    type
  } = props;
  const {
    width,
    height
  } = containerRef.getBoundingClientRect();
  const {
    width: targetWidth,
    height: targetHeight
  } = targetRef.getBoundingClientRect();
  const centerOffsetX = targetWidth / 2;
  const centerOffsetY = targetHeight / 2;
  const saturation = (offset.x + centerOffsetX) / width;
  const bright = 1 - (offset.y + centerOffsetY) / height;
  const hsb = color.toHsb();
  const alphaOffset = saturation;
  const hueOffset = (offset.x + centerOffsetX) / width * 360;
  if (type) {
    switch (type) {
      case 'hue':
        return generateColor((0, _extends2.default)((0, _extends2.default)({}, hsb), {
          h: hueOffset <= 0 ? 0 : hueOffset
        }));
      case 'alpha':
        return generateColor((0, _extends2.default)((0, _extends2.default)({}, hsb), {
          a: alphaOffset <= 0 ? 0 : alphaOffset
        }));
    }
  }
  return generateColor({
    h: hsb.h,
    s: saturation <= 0 ? 0 : saturation,
    b: bright >= 1 ? 1 : bright,
    a: hsb.a
  });
};
exports.calculateColor = calculateColor;
const calcOffset = (color, type) => {
  const hsb = color.toHsb();
  switch (type) {
    case 'hue':
      return {
        x: hsb.h / 360 * 100,
        y: 50
      };
    case 'alpha':
      return {
        x: color.a * 100,
        y: 50
      };
    // Picker panel
    default:
      return {
        x: hsb.s * 100,
        y: (1 - hsb.b) * 100
      };
  }
};
exports.calcOffset = calcOffset;