"use strict";

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

exports.__esModule = true;
exports.saveSchema = exports.resetSchema = exports.preview = exports.getPageSchema = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

var isNewEngineVersion = !!_lowcodeEngine.material;

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

var preview = function preview() {
  saveSchema();
  setTimeout(function () {
    window.open('./preview.html');
  }, 500);
};

exports.preview = preview;

var saveSchema = function saveSchema() {
  window.localStorage.setItem('projectSchema', getCurrentSchema());
  var packages = isNewEngineVersion ? _lowcodeEngine.material.getAssets().packages : _lowcodeEngine.editor.get('assets').packages;
  window.localStorage.setItem('packages', JSON.stringify(packages));

  _message["default"].success('成功保存到本地');
};

exports.saveSchema = saveSchema;

var resetSchema = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var projectSchema;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            projectSchema = {
              "version": "1.0.0",
              "componentsMap": [{
                "devMode": "lowcode",
                "componentName": "Page"
              }],
              "componentsTree": [{
                "componentName": "Page",
                "props": {}
              }]
            };
            window.localStorage.setItem('projectSchema', JSON.stringify(projectSchema));

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

            _message["default"].success('成功重置页面');

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function resetSchema() {
    return _ref.apply(this, arguments);
  };
}();

exports.resetSchema = resetSchema;

var getPageSchema = function getPageSchema() {
  var _schema$componentsTre;

  var schema = JSON.parse(window.localStorage.getItem('projectSchema') || '{}');
  var pageSchema = schema === null || schema === void 0 ? void 0 : (_schema$componentsTre = schema.componentsTree) === null || _schema$componentsTre === void 0 ? void 0 : _schema$componentsTre[0];
  return pageSchema;
};

exports.getPageSchema = getPageSchema;