"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.schemaPaneRegistry = exports["default"] = void 0;

var _button = _interopRequireDefault(require("@alifd/next/lib/button"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _message = _interopRequireDefault(require("@alifd/next/lib/message"));

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

var _lowcodeEngine = require("@alilc/lowcode-engine");

var _lowcodePluginBaseMonacoEditor = _interopRequireDefault(require("@ali/lowcode-plugin-base-monaco-editor"));

require("./index.scss");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

var isNewEngineVersion = !!_lowcodeEngine.material;
var style = {
  height: 'calc(100% - 80px)'
};
var defaultEditorOption = {
  language: 'json',
  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
  },
  scrollbar: {
    vertical: "auto",
    horizontal: "auto"
  }
};

var getCurrentSchema = function getCurrentSchema() {
  return JSON.stringify(isNewEngineVersion ? _lowcodeEngine.project.exportSchema() : _lowcodeEngine.project.getSchema(), null, 2);
};

function SchemaPane(props) {
  if (props && props.width) {
    style.width = props.width;
  }

  var schema = getCurrentSchema();

  var _useState = (0, _react.useState)(schema),
      value = _useState[0],
      setValue = _useState[1];

  (0, _react.useEffect)(function () {
    setValue(getCurrentSchema());
  }, [schema]);

  function onImport() {
    var content = {};

    try {
      content = JSON.parse(value);
    } catch (e) {
      _message["default"].error('schema解析失败');

      return;
    }

    if (isNewEngineVersion) {
      _lowcodeEngine.project.importSchema(content);
    } else {
      _lowcodeEngine.project.load(content, true);
    }

    _message["default"].success('导入 schema 成功');
  }

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "dataProcess"
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: "title"
  }, "\u606D\u559C\u60A8\u53D1\u73B0\u300C\u9AD8\u7EA7\u529F\u80FD\u300D, \u8BF7\u8C28\u614E\u64CD\u4F5C"), /*#__PURE__*/_react["default"].createElement(_lowcodePluginBaseMonacoEditor["default"], (0, _extends2["default"])({}, defaultEditorOption, {
    value: value,
    style: style,
    onChange: setValue
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "actions"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: onImport,
    type: "primary"
  }, "\u5BFC\u5165 Schema")));
}

var schemaPaneRegistry = function schemaPaneRegistry(ctx) {
  return {
    name: "schema-pane-registry",
    init: function init() {
      var schemaDock = ctx.skeleton.add({
        name: "schemaPane",
        area: "leftArea",
        type: "PanelDock",
        props: {
          align: "bottom",
          icon: "code_open",
          description: "页面源码"
        },
        panelProps: {
          width: 600
        },
        content: SchemaPane
      });
      schemaDock && schemaDock.disable();

      if (isNewEngineVersion) {
        _lowcodeEngine.project.onSimulatorRendererReady(function () {
          schemaDock.enable();
        });
      } else {
        _lowcodeEngine.project.onRendererReady(function () {
          schemaDock.enable();
        });
      }
    }
  };
};

exports.schemaPaneRegistry = schemaPaneRegistry;
schemaPaneRegistry.pluginName = 'schemaPaneRegistry';
var _default = schemaPaneRegistry;
exports["default"] = _default;