"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;
};

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

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

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

var color_1 = require("react-color/lib/helpers/color");

var common_1 = require("react-color/lib/components/common");

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

var ColorFields = function ColorFields(_a) {
  var onChange = _a.onChange,
      rgb = _a.rgb,
      hsl = _a.hsl,
      hex = _a.hex;

  var handleChange = function handleChange(data, e) {
    if (data.hex) {
      if (color_1.isValidHex(data.hex)) {
        onChange({
          hex: data.hex,
          source: 'hex'
        }, e);
      }
    } else if (data.r || data.g || data.b) {
      onChange({
        r: data.r || rgb.r,
        g: data.g || rgb.g,
        b: data.b || rgb.b,
        a: rgb.a,
        source: 'rgb'
      }, e);
    } else if (data.a) {
      if (data.a < 0) {
        data.a = 0;
      } else if (data.a > 100) {
        data.a = 100;
      }

      data.a /= 100;
      onChange({
        h: hsl.h,
        s: hsl.s,
        l: hsl.l,
        a: data.a,
        source: 'rgb'
      }, e);
    }
  };

  return jsx_runtime_1.jsxs("div", __assign({
    className: constant_1.prefixClsCustom + "-fileld"
  }, {
    children: [jsx_runtime_1.jsx("div", __assign({
      className: constant_1.prefixClsCustom + "-fileld-double"
    }, {
      children: jsx_runtime_1.jsx(common_1.EditableInput, {
        label: "hex",
        onChange: handleChange,
        value: hex.replace('#', '')
      }, void 0)
    }), void 0), jsx_runtime_1.jsx("div", __assign({
      className: constant_1.prefixClsCustom + "-fileld-single"
    }, {
      children: jsx_runtime_1.jsx(common_1.EditableInput, {
        label: "r",
        value: rgb.r,
        dragLabel: "true",
        dragMax: "255",
        onChange: handleChange
      }, void 0)
    }), void 0), jsx_runtime_1.jsx("div", __assign({
      className: constant_1.prefixClsCustom + "-fileld-single"
    }, {
      children: jsx_runtime_1.jsx(common_1.EditableInput, {
        label: "g",
        value: rgb.g,
        dragLabel: "true",
        dragMax: "255",
        onChange: handleChange
      }, void 0)
    }), void 0), jsx_runtime_1.jsx("div", __assign({
      className: constant_1.prefixClsCustom + "-fileld-single"
    }, {
      children: jsx_runtime_1.jsx(common_1.EditableInput, {
        label: "b",
        value: rgb.b,
        dragLabel: "true",
        dragMax: "255",
        onChange: handleChange
      }, void 0)
    }), void 0), jsx_runtime_1.jsx("div", __assign({
      className: constant_1.prefixClsCustom + "-fileld-alpha"
    }, {
      children: jsx_runtime_1.jsx(common_1.EditableInput, {
        label: "a",
        dragLabel: "true",
        dragMax: "100",
        onChange: handleChange,
        value: Math.round((rgb.a || 0) * 100)
      }, void 0)
    }), void 0)]
  }), void 0);
};

exports.ColorFields = ColorFields;
exports.ColorFields.displayName = 'ColorFields';
exports["default"] = exports.ColorFields;
var ColorFieldsMemo = React.memo(exports.ColorFields);
exports.ColorFieldsMemo = ColorFieldsMemo;
ColorFieldsMemo.displayName = 'ColorFieldsMemo';

