'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var fs = require('fs');
var path = require('path');
var axios = require('axios');
var fs_collectDirFiles = require('../fs/collect-dir-files.js');
require('../nodejs/fs.js');
require('../nodejs/path.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n["default"] = e;
    return Object.freeze(n);
}

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
var path__namespace = /*#__PURE__*/_interopNamespace(path);
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);

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; }
var BASE_URL = 'https://pages.woa.com';
var MAX_BATCH_SIZE = 4 * 1024 * 1024; // 4MB，留 1MB 余量给 JSON 结构
// 二进制文件扩展名（需要 base64 编码）
var BINARY_EXTS = new Set(['.ico', '.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.avif', '.woff', '.woff2', '.ttf', '.eot', '.otf', '.mp3', '.mp4', '.webm', '.ogg', '.wav', '.zip', '.gz', '.br', '.tar', '.rar', '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.wasm']);
/**
 * 补全域名后缀
 */
function normalizeCname(cname) {
  if (!cname.includes('.')) {
    return "".concat(cname, ".pages.woa.com");
  }
  if (!cname.endsWith('.woa.com')) {
    return "".concat(cname, ".pages.woa.com");
  }
  return cname;
}
/**
 * 将 collectFilesSync 返回的绝对路径列表转换为 FileEntry 列表
 */
function toFileEntries(filePaths, baseDir) {
  return filePaths.map(function (fullPath) {
    return {
      fullPath: fullPath,
      relativePath: path__namespace.relative(baseDir, fullPath)
    };
  });
}
/**
 * 读取文件内容，二进制文件自动 base64 编码
 */
function readFileContent(filePath) {
  var ext = path__namespace.extname(filePath).toLowerCase();
  if (BINARY_EXTS.has(ext)) {
    return fs__namespace.readFileSync(filePath).toString('base64');
  }
  return fs__namespace.readFileSync(filePath, 'utf-8');
}
/**
 * 将文件列表按大小分批
 */
function splitIntoBatches(files) {
  var batches = [];
  var currentBatch = {};
  var currentSize = 0;
  var _iterator = _createForOfIteratorHelper(files),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var file = _step.value;
      var content = readFileContent(file.fullPath);
      var entryJson = JSON.stringify(_defineProperty__default["default"]({}, file.relativePath, content));
      var size = Buffer.byteLength(entryJson);
      // 单个文件超过限制，单独一批
      if (size > MAX_BATCH_SIZE) {
        if (Object.keys(currentBatch).length > 0) {
          batches.push(currentBatch);
          currentBatch = {};
          currentSize = 0;
        }
        batches.push(_defineProperty__default["default"]({}, file.relativePath, content));
        continue;
      }
      if (currentSize + size > MAX_BATCH_SIZE && Object.keys(currentBatch).length > 0) {
        batches.push(currentBatch);
        currentBatch = {};
        currentSize = 0;
      }
      currentBatch[file.relativePath] = content;
      currentSize += size;
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  if (Object.keys(currentBatch).length > 0) {
    batches.push(currentBatch);
  }
  return batches;
}
/**
 * 发送请求到 OA Pages API
 */
function request(method, urlPath, data, apiKey) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var res;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return axios__default["default"]({
            method: method,
            url: "".concat(BASE_URL).concat(urlPath),
            data: data,
            headers: {
              'X-Api-Key': apiKey
            },
            validateStatus: function validateStatus() {
              return true;
            },
            maxBodyLength: Infinity,
            maxContentLength: Infinity
          });
        case 1:
          res = _context.sent;
          return _context.abrupt("return", {
            status: res.status,
            data: res.data
          });
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
/**
 * 检查网站是否已存在
 */
function checkSiteExists(cname, apiKey) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var res;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return request('GET', "/api/repos/".concat(cname), {}, apiKey);
        case 1:
          res = _context2.sent;
          return _context2.abrupt("return", res.status === 200);
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
/**
 * 部署本地目录到 OA Pages
 *
 * @param options - 部署配置
 * @returns 部署结果
 *
 * @example
 * ```ts
 * import { deployPages } from 't-comm';
 *
 * // 基本用法
 * const result = await deployPages({
 *   cname: 't-comm.pages.woa.com',
 *   directory: 'docs/.vitepress/dist',
 *   visibility: 'public',
 * });
 *
 * // 传入 API Key（不依赖环境变量）
 * const result = await deployPages({
 *   cname: 't-comm',
 *   directory: './dist',
 *   apiKey: 'oa-pages-key-xxxxxxxx',
 *   visibility: 'tof',
 *   description: '我的网站',
 * });
 * ```
 */
function deployPages(options) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var directory, visibility, description, dryRun, apiKey, cname, distDir, filePaths, files, batches, result, exists, i, res, createBody, createRes, _i, _res, patchRes;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          directory = options.directory, visibility = options.visibility, description = options.description, dryRun = options.dryRun; // 获取 API Key：优先使用传入参数，其次环境变量
          apiKey = options.apiKey || process.env.OA_PAGES_API_KEY;
          if (apiKey) {
            _context3.next = 1;
            break;
          }
          return _context3.abrupt("return", {
            success: false,
            url: '',
            adminUrl: '',
            cname: '',
            totalFiles: 0,
            totalBatches: 0,
            error: '未提供 apiKey 参数，且环境变量 OA_PAGES_API_KEY 未配置。请访问 https://pages.woa.com/admin 申请 API Key'
          });
        case 1:
          // 补全域名
          cname = normalizeCname(options.cname); // 解析目录路径
          distDir = path__namespace.resolve(directory);
          if (fs__namespace.existsSync(distDir)) {
            _context3.next = 2;
            break;
          }
          return _context3.abrupt("return", {
            success: false,
            url: '',
            adminUrl: '',
            cname: cname,
            totalFiles: 0,
            totalBatches: 0,
            error: "\u76EE\u5F55\u4E0D\u5B58\u5728: ".concat(distDir)
          });
        case 2:
          if (fs__namespace.statSync(distDir).isDirectory()) {
            _context3.next = 3;
            break;
          }
          return _context3.abrupt("return", {
            success: false,
            url: '',
            adminUrl: '',
            cname: cname,
            totalFiles: 0,
            totalBatches: 0,
            error: "\u8DEF\u5F84\u4E0D\u662F\u76EE\u5F55: ".concat(distDir)
          });
        case 3:
          // 使用 collectFilesSync 递归获取所有文件
          filePaths = fs_collectDirFiles.collectFilesSync(distDir);
          if (!(filePaths.length === 0)) {
            _context3.next = 4;
            break;
          }
          return _context3.abrupt("return", {
            success: false,
            url: '',
            adminUrl: '',
            cname: cname,
            totalFiles: 0,
            totalBatches: 0,
            error: '目录为空，无文件可部署'
          });
        case 4:
          // 转换为 FileEntry 格式
          files = toFileEntries(filePaths, distDir); // 分批
          batches = splitIntoBatches(files);
          result = {
            success: true,
            url: "https://".concat(cname),
            adminUrl: "https://pages.woa.com/admin/".concat(cname),
            cname: cname,
            totalFiles: files.length,
            totalBatches: batches.length
          };
          if (!dryRun) {
            _context3.next = 5;
            break;
          }
          return _context3.abrupt("return", result);
        case 5:
          _context3.next = 6;
          return checkSiteExists(cname, apiKey);
        case 6:
          exists = _context3.sent;
          if (!exists) {
            _context3.next = 11;
            break;
          }
          i = 0;
        case 7:
          if (!(i < batches.length)) {
            _context3.next = 10;
            break;
          }
          _context3.next = 8;
          return request('PUT', "/api/sites/".concat(cname), {
            files: batches[i]
          }, apiKey);
        case 8:
          res = _context3.sent;
          if (!(res.status !== 200)) {
            _context3.next = 9;
            break;
          }
          result.success = false;
          result.error = "\u66F4\u65B0\u7B2C ".concat(i + 1, " \u6279\u6587\u4EF6\u5931\u8D25: ").concat(res.status, " ").concat(JSON.stringify(res.data));
          return _context3.abrupt("return", result);
        case 9:
          i++;
          _context3.next = 7;
          break;
        case 10:
          _context3.next = 17;
          break;
        case 11:
          // 网站不存在，使用 POST 创建
          createBody = {
            cname: cname,
            files: batches[0]
          };
          if (description) {
            createBody.description = description;
          }
          _context3.next = 12;
          return request('POST', '/api/sites', createBody, apiKey);
        case 12:
          createRes = _context3.sent;
          if (!(createRes.status !== 200 && createRes.status !== 201)) {
            _context3.next = 13;
            break;
          }
          result.success = false;
          result.error = "\u521B\u5EFA\u7F51\u7AD9\u5931\u8D25: ".concat(createRes.status, " ").concat(JSON.stringify(createRes.data));
          return _context3.abrupt("return", result);
        case 13:
          _i = 1;
        case 14:
          if (!(_i < batches.length)) {
            _context3.next = 17;
            break;
          }
          _context3.next = 15;
          return request('PUT', "/api/sites/".concat(cname), {
            files: batches[_i]
          }, apiKey);
        case 15:
          _res = _context3.sent;
          if (!(_res.status !== 200)) {
            _context3.next = 16;
            break;
          }
          result.success = false;
          result.error = "\u66F4\u65B0\u7B2C ".concat(_i + 1, " \u6279\u6587\u4EF6\u5931\u8D25: ").concat(_res.status, " ").concat(JSON.stringify(_res.data));
          return _context3.abrupt("return", result);
        case 16:
          _i++;
          _context3.next = 14;
          break;
        case 17:
          if (!visibility) {
            _context3.next = 19;
            break;
          }
          _context3.next = 18;
          return request('PATCH', "/api/repos/".concat(cname), {
            visibility: visibility
          }, apiKey);
        case 18:
          patchRes = _context3.sent;
          if (patchRes.status !== 200) {
            result.error = "\u6743\u9650\u8BBE\u7F6E\u5931\u8D25\uFF08\u4F46\u6587\u4EF6\u5DF2\u4E0A\u4F20\u6210\u529F\uFF09: ".concat(patchRes.status, " ").concat(JSON.stringify(patchRes.data));
          }
        case 19:
          return _context3.abrupt("return", result);
        case 20:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

exports.deployPages = deployPages;
