"use strict";

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

var _omit2 = _interopRequireDefault(require("lodash/omit"));

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

var _react2 = require("@emotion/react");

var _immutable = require("immutable");

var _index = require("../index");

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }

var _ref = process.env.NODE_ENV === "production" ? {
  name: "1xfnuhy-Shortcode",
  styles: "margin-top:0;margin-bottom:16px;&:first-of-type{margin-top:0;};label:Shortcode;"
} : {
  name: "1xfnuhy-Shortcode",
  styles: "margin-top:0;margin-bottom:16px;&:first-of-type{margin-top:0;};label:Shortcode;",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3NyYy93aWRnZXRzL21hcmtkb3duL01hcmtkb3duQ29udHJvbC9jb21wb25lbnRzL1Nob3J0Y29kZS5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUF3Q29CIiwiZmlsZSI6Ii4uLy4uLy4uLy4uLy4uLy4uL3NyYy93aWRnZXRzL21hcmtkb3duL01hcmtkb3duQ29udHJvbC9jb21wb25lbnRzL1Nob3J0Y29kZS5qcyIsInNvdXJjZXNDb250ZW50IjpbIi8qIGVzbGludC1kaXNhYmxlIHJlYWN0L3Byb3AtdHlwZXMgKi9cbmltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCc7XG5pbXBvcnQgeyBjc3MgfSBmcm9tICdAZW1vdGlvbi9yZWFjdCc7XG5pbXBvcnQgeyBNYXAsIGZyb21KUyB9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9ydGNvZGVOZXcnKX1cbiAgICAgICAgICAgIGlzU2VsZWN0ZWQ9e2VkaXRvci5pc1NlbGVjdGVkKG5vZGUpfVxuICAgICAgICAgIC8+XG4gICAgICAgIDwvZGl2PlxuICAgICAgKVxuICAgICk7XG4gIH1cbn1cbiJdfQ== */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
};

class Shortcode extends _react.default.Component {
  constructor() {
    super(...arguments);

    _defineProperty(this, "state", {
      field: (0, _immutable.Map)()
    });
  }

  componentDidMount() {
    const {
      node,
      typeOverload
    } = this.props;
    const plugin = (0, _index.getEditorComponents)().get(typeOverload || node.data.get('shortcode'));
    const fieldKeys = ['id', 'fromBlock', 'toBlock', 'toPreview', 'pattern', 'icon'];
    const field = (0, _immutable.fromJS)((0, _omit2.default)(plugin, fieldKeys));
    this.setState({
      field
    });
  }

  render() {
    const {
      editor,
      node,
      dataKey = 'shortcodeData'
    } = this.props;
    const {
      field
    } = this.state;
    const EditorControl = (0, _index.getEditorControl)();
    const value = dataKey === false ? node.data : (0, _immutable.fromJS)(node.data.get(dataKey));

    function handleChange(fieldName, value, metadata) {
      const dataValue = dataKey === false ? value : node.data.set('shortcodeData', value);
      editor.setNodeByKey(node.key, {
        data: dataValue || (0, _immutable.Map)(),
        metadata
      });
    }

    function handleFocus() {
      return editor.moveToRangeOfNode(node);
    }

    return !field.isEmpty() && (0, _react2.jsx)("div", {
      onClick: handleFocus,
      onFocus: handleFocus
    }, (0, _react2.jsx)(EditorControl, {
      css: _ref,
      value: value,
      field: field,
      onChange: handleChange,
      isEditorComponent: true,
      isNewEditorComponent: node.data.get('shortcodeNew'),
      isSelected: editor.isSelected(node)
    }));
  }

}

exports.default = Shortcode;