"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.saveSchema = exports.preview = exports.getProjectSchemaFromLocalStorage = exports.getPackagesFromLocalStorage = exports.getActualWidthOfChars = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _lowcodePluginInject = require("@alilc/lowcode-plugin-inject");
var _lowcodeEngine = require("@alilc/lowcode-engine");
var _lowcodeTypes = require("@alilc/lowcode-types");
// import { Message } from '@alifd/next';

var getLSName = function getLSName(scenarioName, ns) {
  if (ns === void 0) {
    ns = 'projectSchema';
  }
  return scenarioName + ":" + ns;
};
var getProjectSchemaFromLocalStorage = function getProjectSchemaFromLocalStorage(scenarioName) {
  if (!scenarioName) {
    console.error('scenarioName is required!');
    return;
  }
  return JSON.parse(window.localStorage.getItem(getLSName(scenarioName)) || '{}');
};
exports.getProjectSchemaFromLocalStorage = getProjectSchemaFromLocalStorage;
var getPackagesFromLocalStorage = function getPackagesFromLocalStorage(scenarioName) {
  if (!scenarioName) {
    console.error('scenarioName is required!');
    return;
  }
  return JSON.parse(window.localStorage.getItem(getLSName(scenarioName, 'packages')) || '{}');
};
exports.getPackagesFromLocalStorage = getPackagesFromLocalStorage;
var setProjectSchemaToLocalStorage = function setProjectSchemaToLocalStorage(scenarioName) {
  if (!scenarioName) {
    console.error('scenarioName is required!');
    return;
  }
  window.localStorage.setItem(getLSName(scenarioName), JSON.stringify(_lowcodeEngine.project.exportSchema(_lowcodeTypes.TransformStage.Save)));
};
var preview = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(scenarioName) {
    var search, result;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (scenarioName === void 0) {
            scenarioName = 'index';
          }
          _context.next = 3;
          return saveSchema(scenarioName);
        case 3:
          search = location.search ? location.search + "&scenarioName=" + scenarioName : "?scenarioName=" + scenarioName;
          result = "./preview.html" + search;
          return _context.abrupt("return", {
            url: result
          });
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function preview(_x) {
    return _ref.apply(this, arguments);
  };
}();
exports.preview = preview;
var saveSchema = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(scenarioName) {
    var packages;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          if (scenarioName === void 0) {
            scenarioName = 'index';
          }
          setProjectSchemaToLocalStorage(scenarioName);
          _context2.next = 4;
          return setPackgesToLocalStorage(scenarioName);
        case 4:
          window.localStorage.setItem('projectSchema', JSON.stringify(_lowcodeEngine.project.exportSchema(_lowcodeTypes.TransformStage.Save)));
          _context2.next = 7;
          return (0, _lowcodePluginInject.filterPackages)(_lowcodeEngine.material.getAssets().packages);
        case 7:
          packages = _context2.sent;
          window.localStorage.setItem('packages', JSON.stringify(packages));
          // Message.success('成功保存到本地');
        case 9:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return function saveSchema(_x2) {
    return _ref2.apply(this, arguments);
  };
}();
exports.saveSchema = saveSchema;
var setPackgesToLocalStorage = /*#__PURE__*/function () {
  var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(scenarioName) {
    var packages;
    return _regenerator["default"].wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          if (scenarioName) {
            _context3.next = 2;
            break;
          }
          return _context3.abrupt("return");
        case 2:
          _context3.next = 4;
          return (0, _lowcodePluginInject.filterPackages)(_lowcodeEngine.material.getAssets().packages);
        case 4:
          packages = _context3.sent;
          window.localStorage.setItem(getLSName(scenarioName, 'packages'), JSON.stringify(packages));
        case 6:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return function setPackgesToLocalStorage(_x3) {
    return _ref3.apply(this, arguments);
  };
}();

/**
 * 获取元素实际宽度
 * @param text
 * @param options
 * @returns
 */
var getActualWidthOfChars = function getActualWidthOfChars(text, _ref4) {
  var _ref4$size = _ref4.size,
    size = _ref4$size === void 0 ? 12 : _ref4$size,
    _ref4$family = _ref4.family,
    family = _ref4$family === void 0 ? 'Microsoft YaHei' : _ref4$family;
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  ctx.font = size + "px " + family; // 定义字体
  var _ctx$measureText = ctx.measureText(text),
    actualBoundingBoxLeft = _ctx$measureText.actualBoundingBoxLeft,
    actualBoundingBoxRight = _ctx$measureText.actualBoundingBoxRight,
    width = _ctx$measureText.width; // 字体宽度
  var actual = Math.abs(actualBoundingBoxLeft) + Math.abs(actualBoundingBoxRight);
  return Math.max(width, actual);
};
exports.getActualWidthOfChars = getActualWidthOfChars;