import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _Message from "@alifd/next/es/message";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { editor, project, material } from '@ali/lowcode-engine';
var isNewEngineVersion = !!material;

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

export var preview = function preview() {
  saveSchema();
  setTimeout(function () {
    window.open('./preview.html');
  }, 500);
};
export var saveSchema = function saveSchema() {
  window.localStorage.setItem('projectSchema', getCurrentSchema());
  var packages = isNewEngineVersion ? material.getAssets().packages : editor.get('assets').packages;
  window.localStorage.setItem('packages', JSON.stringify(packages));

  _Message.success('成功保存到本地');
};
export var resetSchema = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var projectSchema;
    return _regeneratorRuntime.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) {
              project.importSchema(projectSchema);
            } else {
              project.load(projectSchema, true);
            }

            _Message.success('成功重置页面');

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

  return function resetSchema() {
    return _ref.apply(this, arguments);
  };
}();
export 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;
};