"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
var _lowcodePluginBaseMonacoEditor = _interopRequireDefault(require("@alilc/lowcode-plugin-base-monaco-editor"));
var _customIcon = _interopRequireDefault(require("../../components/custom-icon"));
var _jsBeautify = require("js-beautify");
var _locale = require("./locale");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
// import PropTypes from 'prop-types';

var defaultEditorOption = {
  width: '100%',
  height: '400px',
  readOnly: false,
  automaticLayout: true,
  folding: true,
  // 默认开启折叠代码功能
  lineNumbers: 'on',
  wordWrap: 'off',
  formatOnPaste: true,
  fontSize: 12,
  tabSize: 2,
  scrollBeyondLastLine: false,
  fixedOverflowWidgets: false,
  snippetSuggestions: 'top',
  minimap: {
    enabled: false
  },
  throttle: 0,
  scrollbar: {
    vertical: 'auto',
    horizontal: 'auto'
  }
};
var JsonSetter = exports["default"] = /*#__PURE__*/function (_PureComponent) {
  function JsonSetter() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _PureComponent.call.apply(_PureComponent, [this].concat(args)) || this;
    _this.datasourceCode = '';
    _this.state = {
      isShowDialog: false,
      value: JSON.stringify(_this.props.value)
    };
    _this.openDialog = function () {
      var value = _this.state.value;
      _this.setState({
        isShowDialog: true
      });
      _this.datasourceCode = value;
    };
    _this.closeDialog = function () {
      _this.setState({
        isShowDialog: false
      });
    };
    /**
     * 渲染按钮
     */
    _this.renderButton = function (value) {
      return !value ? /*#__PURE__*/_react["default"].createElement(_button["default"], {
        size: "small",
        type: "normal",
        onClick: _this.openDialog
      }, (0, _locale.intl)('BindingData')) : /*#__PURE__*/_react["default"].createElement(_button["default"], {
        size: "small",
        type: "primary",
        onClick: _this.openDialog
      }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: "edit"
      }), (0, _locale.intl)('EditData'));
    };
    _this.updateCode = function (newCode) {
      _this.datasourceCode = newCode;
    };
    _this.onDialogOk = function () {
      var _this$props = _this.props,
        onChange = _this$props.onChange,
        removeProp = _this$props.removeProp;
      if (_this.datasourceCode && _this.datasourceCode != '') {
        try {
          onChange(JSON.parse(_this.datasourceCode));
          _this.closeDialog();
        } catch (e) {
          _dialog["default"].alert({
            title: (0, _locale.intl)('FailedToSaveData'),
            content: e.message
          });
        }
      } else {
        removeProp();
        _this.closeDialog();
      }
    };
    /**
     * 渲染编辑函数按钮(可直接编辑函数内容)
     */
    _this.renderEditFunctionButton = function () {
      return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_button["default"], {
        size: "small",
        type: "primary",
        onClick: _this.openDialog
      }, /*#__PURE__*/_react["default"].createElement(_customIcon["default"], {
        type: "icon-ic_edit"
      }), (0, _locale.intl)('EditData')));
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(JsonSetter, _PureComponent);
  var _proto = JsonSetter.prototype;
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var nextValue = JSON.stringify(nextProps.value);
    if (nextValue !== this.state.value) {
      this.setState({
        value: nextValue
      });
    }
  };
  _proto.render = function render() {
    var _this2 = this;
    var value = this.state.value;
    var isShowDialog = this.state.isShowDialog;
    return /*#__PURE__*/_react["default"].createElement("div", null, this.renderButton(value), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
      visible: isShowDialog,
      closeable: 'close',
      title: (0, _locale.intl)('EditData'),
      onCancel: this.closeDialog,
      onOk: this.onDialogOk,
      onClose: function onClose() {
        _this2.closeDialog();
      },
      cancelProps: {
        children: (0, _locale.intl)('Cancel')
      },
      okProps: {
        children: (0, _locale.intl)('Confirm')
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        width: '500px',
        height: '400px'
      }
    }, /*#__PURE__*/_react["default"].createElement(_lowcodePluginBaseMonacoEditor["default"], (0, _extends2["default"])({
      value: (0, _jsBeautify.js_beautify)(value)
    }, defaultEditorOption, {
      language: 'json',
      onChange: function onChange(newCode) {
        return _this2.updateCode(newCode);
      }
    })))));
  };
  return JsonSetter;
}(_react.PureComponent);
JsonSetter.displayName = 'JsonSetter';