"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _licitUiCommands = require("@modusoperandi/licit-ui-commands");
var _MathQuillEditor = _interopRequireDefault(require("./mathquill-editor/MathQuillEditor"));
var _uuid = _interopRequireDefault(require("./uuid"));
require("./czi-form.css");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
class MathEditor extends React.PureComponent {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      initialValue: this.props.initialValue,
      value: this.props.initialValue || ''
    });
    _defineProperty(this, "_id", (0, _uuid.default)());
    _defineProperty(this, "_unmounted", false);
    _defineProperty(this, "_onChange", value => {
      this.setState({
        value
      });
    });
    _defineProperty(this, "_cancel", () => {
      this.props.close();
    });
    _defineProperty(this, "_insert", () => {
      this.props.close(this.state.value);
    });
  }
  render() {
    const {
      initialValue,
      value
    } = this.state;
    return /*#__PURE__*/React.createElement("div", {
      className: "czi-math-editor"
    }, /*#__PURE__*/React.createElement("form", {
      className: "czi-form",
      onSubmit: _licitUiCommands.preventEventDefault
    }, /*#__PURE__*/React.createElement("fieldset", null, /*#__PURE__*/React.createElement("legend", null, "Insert Math"), /*#__PURE__*/React.createElement(_MathQuillEditor.default, {
      onChange: this._onChange,
      value: value
    })), /*#__PURE__*/React.createElement("div", {
      className: "czi-form-buttons"
    }, /*#__PURE__*/React.createElement(_licitUiCommands.CustomButton, {
      label: "Cancel",
      onClick: this._cancel
    }), /*#__PURE__*/React.createElement(_licitUiCommands.CustomButton, {
      active: true,
      disabled: !this.state.value,
      label: initialValue ? 'Update' : 'Insert',
      onClick: this._insert
    }))));
  }
}
_defineProperty(MathEditor, "propsTypes", {
  initialValue: _propTypes.default.string,
  close: function (props, propName) {
    const fn = props[propName];
    if (!fn.prototype || typeof fn.prototype.constructor !== 'function' && fn.prototype.constructor.length !== 1) {
      return new Error(propName + 'must be a function with 1 arg of type string');
    }
    return null;
  }
});
var _default = MathEditor;
exports.default = _default;