"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _icons = require("@parallel-line/icons");

var _Button = require("./Button");

var _utils = require("../utils");

var _slate = require("../slate");

var _colorBoard = _interopRequireDefault(require("../../color-board"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var FontColorButton = function FontColorButton(_ref) {
  var prefixCls = _ref.prefixCls,
      disabled = _ref.disabled;
  var editor = (0, _slate.useSlate)();

  var marks = _slate.Editor.marks(editor);

  var _React$useState = _react.default.useState(_utils.DEFAULTCOLOR),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      currentColor = _React$useState2[0],
      setColor = _React$useState2[1];

  var style = _react.default.useMemo(function () {
    var style = {};

    if (currentColor === _utils.DEFAULTCOLOR || currentColor === 'currentcolor') {
      return style;
    }

    style.color = currentColor;
    return style;
  }, [currentColor]);

  _react.default.useEffect(function () {
    setColor((marks === null || marks === void 0 ? void 0 : marks.color) || _utils.DEFAULTCOLOR);
  }, [marks === null || marks === void 0 ? void 0 : marks.color]);

  var handleChange = function handleChange(currentColor) {
    setColor(currentColor);

    _slate.Editor.addMark(editor, 'color', currentColor);
  };

  var render = function render() {
    return /*#__PURE__*/_react.default.createElement(_Button.Button, {
      title: "\u5B57\u4F53\u989C\u8272",
      disabled: disabled,
      prefixCls: prefixCls,
      style: style,
      onMouseDown: function onMouseDown(e) {
        e.preventDefault();
      }
    }, /*#__PURE__*/_react.default.createElement(_icons.FontColorsOutlined, null));
  };

  if (disabled) return render();
  return /*#__PURE__*/_react.default.createElement(_colorBoard.default.Picker, {
    defaultColor: _utils.DEFAULTCOLOR,
    currentColor: currentColor,
    onChange: handleChange
  }, render());
};

exports.FontColorButton = FontColorButton;