'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var nodejs_fs = require('../nodejs/fs.js');
var nodejs_path = require('../nodejs/path.js');
var syncCos_helper = require('./helper.js');
require('../fs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function pushCopyFilesInSyncCos(_ref) {
  var fileList = _ref.fileList,
    cos = _ref.cos,
    workspace = _ref.workspace;
  var backupFileList = JSON.parse(JSON.stringify(fileList));
  var domainDir = cos.domainDir,
    _cos$copyList = cos.copyList,
    copyList = _cos$copyList === void 0 ? [] : _cos$copyList;
  if (!(copyList === null || copyList === void 0 ? void 0 : copyList.length)) return;
  var _iterator = _createForOfIteratorHelper(copyList),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var copyInfo = _step.value;
      if (!(copyInfo === null || copyInfo === void 0 ? void 0 : copyInfo.source)) {
        continue;
      }
      var source = copyInfo.source,
        copyTargetPrefix = copyInfo.copyTargetPrefix;
      var baseDir = "".concat(domainDir, "/").concat(source);
      var copyTargetDirs = syncCos_helper.getCopyTargetDirs(nodejs_path.getPath().resolve(workspace, baseDir), nodejs_path.getPath().resolve(workspace, domainDir), {
        copyTargetPrefix: copyTargetPrefix
      });
      console.log('[copyTargetDirs]', copyTargetDirs);
      if (!(copyTargetDirs === null || copyTargetDirs === void 0 ? void 0 : copyTargetDirs.length)) {
        continue;
      }
      var _iterator2 = _createForOfIteratorHelper(backupFileList),
        _step2;
      try {
        for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
          var item = _step2.value;
          // 只处理当前域名、当前业务、当前语言的
          if (!item.relativePath.startsWith("".concat(baseDir, "/"))) {
            continue;
          }
          var _iterator3 = _createForOfIteratorHelper(copyTargetDirs),
            _step3;
          try {
            for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
              var extra = _step3.value;
              var newPath = item.relativePath.replace("".concat(baseDir, "/"), "".concat(domainDir, "/").concat(extra, "/"));
              var fullPath = nodejs_path.getPath().resolve(workspace, newPath);
              if (!nodejs_fs.getFs().existsSync(fullPath)) {
                var key = newPath.replace("".concat(domainDir, "/"), '');
                fileList.push({
                  path: item.path,
                  relativePath: item.relativePath,
                  key: key,
                  url: "".concat(cos.cdnPrefix, "/").concat(key)
                });
              }
            }
          } catch (err) {
            _iterator3.e(err);
          } finally {
            _iterator3.f();
          }
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
}
function pushMainlandCosFilesInSyncCos(_ref2) {
  var fileList = _ref2.fileList,
    cos = _ref2.cos,
    mainLandCdnInfo = _ref2.mainLandCdnInfo,
    toPushMainlandFiles = _ref2.toPushMainlandFiles;
  var domainDir = cos.domainDir;
  if (domainDir === mainLandCdnInfo.domainDir) return;
  if (!mainLandCdnInfo) {
    console.log('not found mainLandCdnInfo');
    return;
  }
  var _iterator4 = _createForOfIteratorHelper(fileList),
    _step4;
  try {
    for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
      var item = _step4.value;
      var key = item.key,
        path = item.path,
        relativePath = item.relativePath;
      // 不应该用 path，path 是最原始，可能永远是 zh-cn
      // 不能当 key
      // const newPath = path.replace(path.resolve(workspace, domainDir), '').replace(/^\//, '');
      toPushMainlandFiles.push({
        path: path,
        relativePath: relativePath,
        key: key,
        url: "".concat(mainLandCdnInfo.cdnPrefix, "/").concat(key)
      });
    }
  } catch (err) {
    _iterator4.e(err);
  } finally {
    _iterator4.f();
  }
}

exports.pushCopyFilesInSyncCos = pushCopyFilesInSyncCos;
exports.pushMainlandCosFilesInSyncCos = pushMainlandCosFilesInSyncCos;
