"use strict";

var _WeakMap = require("@babel/runtime-corejs2/core-js/weak-map");
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
  value: true
});
exports.add = add;
exports.check = check;
exports.get = get;
exports.getWorkersPath = getWorkersPath;
exports.update = update;
exports.updateWorkerToSubPkg = updateWorkerToSubPkg;
var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));
var _index = require("../../cli-shared-utils/index.js");
var constanst = _interopRequireWildcard(require("./constanst.js"));
var paths = _interopRequireWildcard(require("./paths.js"));
var _path = _interopRequireDefault(require("path"));
function _getRequireWildcardCache(e) { if ("function" != typeof _WeakMap) return null; var r = new _WeakMap(), t = new _WeakMap(); return (_getRequireWildcardCache = function (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; }
/**
 * 校验当前工程是否存在必要的文件
 */
function check() {
  if (!_index.fs.existsSync(paths.MANIFEST)) {
    (0, _index.error)(`请确认项目${paths.SRC}下存在文件：${paths.MANIFEST}`);
    throw new Error(`请确认项目${paths.SRC}下存在文件：${paths.MANIFEST}`);
  }
  if (!_index.fs.existsSync(paths.ENTRY)) {
    (0, _index.error)(`请确认项目${paths.SRC}下存在文件：${paths.ENTRY}`);
    throw new Error(`请确认项目${paths.SRC}下存在文件：${paths.ENTRY}`);
  }
}
function get() {
  try {
    const config = _index.fs.readJsonSync(paths.MANIFEST);

    // 更新orientation字段
    let hasUpdate = false;
    if (config && config.deviceOrientation && !config.orientation) {
      config.orientation = config.deviceOrientation;
      hasUpdate = true;
    }
    if (hasUpdate) {
      _index.fs.outputJsonSync(paths.MANIFEST, config);
    }
    return updateWorkerToSubPkg(config);
  } catch (err) {
    (0, _index.error)(`读取小游戏配置文件出错！`);
    (0, _index.error)(err);
    return {};
  }
}

/**
 * 将workers配置到分包中
 */
function updateWorkerToSubPkg(manifest) {
  if (!manifest || !manifest.workers || !manifest.workers.path || !manifest.workers.isSubpackage) {
    return manifest;
  }
  if (manifest.subpackages) {
    // 查找是否已经包含
    const hasWorkersPackage = manifest.subpackages.find(pack => pack.name === 'workers') !== undefined;
    if (hasWorkersPackage) {
      return manifest;
    }
  } else {
    manifest.subpackages = [];
  }

  // 添加一个新的子包对象到sub数组中
  manifest.subpackages.push({
    name: 'workers',
    root: manifest.workers.path
  });

  // 更新本地manifest
  try {
    _index.fs.outputJsonSync(paths.MANIFEST, manifest);
    const workersPath = _path.default.resolve(paths.SRC, manifest.workers.path);
    const workersGameJs = _path.default.resolve(workersPath, 'game.js');
    if (_index.fs.existsSync(workersPath) && !_index.fs.existsSync(workersGameJs)) {
      _index.fs.writeFileSync(workersGameJs, '');
    }
  } catch (error) {
    error(error);
  }
  return manifest;
}

/**
 * 获取workers路径
 */
function getWorkersPath() {
  const manifest = get();
  if (!manifest || !manifest.workers) {
    return '';
  }
  if (manifest.workers.path) {
    return _path.default.resolve(paths.SRC, manifest.workers.path);
  }
  return _path.default.resolve(paths.SRC, manifest.workers);
}

/**
 * 更新/添加 manifest 字段
 * @param {Buffer} content manifest.json的二进制内容
 * @param {Boolean} release是否是release版本
 */
function add(key, value) {
  const config = get();
  config[key] = value;
  try {
    _index.fs.outputJsonSync(paths.MANIFEST, config, {
      spaces: 2
    });
  } catch (error) {
    error(error);
  }
}

/**
 * 更新manifest文件
 * @param {Buffer} content manifest.json的二进制内容
 * @param {Boolean} release是否是release版本
 */
function update({
  content,
  release
}) {
  let manifest;
  try {
    manifest = JSON.parse(content.toString());
  } catch (err) {
    (0, _index.error)(`解析${constanst.MANIFEST}文件出错`);
    throw err;
  }

  // 更新config.debug
  manifest.config = manifest.config || {};
  manifest.config.debug = !release;

  // 更新最小版本号
  if (!manifest.minPlatformVersion) {
    manifest.minPlatformVersion = 101;
  }

  // 联盟版本号写入
  if (!manifest.allianceVersion) {
    (0, _index.info)('联盟包体需要将manifest中allianceVersion & minPlatformVersion进行写入');
    manifest.allianceVersion = 1300;
    manifest.minPlatformVersion = 1000;
    if (manifest.plugins) {
      manifest.plugins = {};
    }
  }

  // 更新内容
  return (0, _stringify.default)(manifest, null, 2);
}