"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const FS = __importStar(require("fs"));
const Path = __importStar(require("path"));
const Urlencode = __importStar(require("urlencode"));
const Utils = __importStar(require("../../../util/src"));
const Model = __importStar(require("../model"));
function build(path) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!FS.existsSync(Path.join(path, 'dist/native'))) {
            return Promise.reject(Error(`未找到可提取的cube编译产物`));
        }
        const cases = new Array();
        Utils.Console.info(`提取工程配置 [${Path.join(path, 'dist/package/package.json')}]`);
        const pkg = JSON.parse(FS.readFileSync(Path.join(path, 'dist/package/package.json')).toString());
        Utils.Console.info(`提取应用配置 [${Path.join(path, 'dist/native/appConfig.json')}]`);
        const app = JSON.parse(FS.readFileSync(Path.join(path, 'dist/native/appConfig.json')).toString());
        const srcPath = Path.join(path, `_rubik.zip`);
        Utils.FS.rmdirs(srcPath);
        yield Utils.FS.zip(Path.join(path, `dist/native`), srcPath);
        //  TODO: check case version
        const srcUrl = yield Utils.Basement.upload(srcPath, `${pkg.name}_${pkg.version}`);
        Utils.FS.rmdirs(srcPath);
        let index = 1;
        for (const page of app.pages) {
            Utils.Console.info(`构建页面用例 [${page}] [${index}/${app.pages.length}]`);
            const acase = Model.Case.defaultCase();
            acase.poles = Model.Case.Pole.Snapshot;
            acase.type = Model.Case.Type.Tiny;
            acase.subtype = Model.Case.Subtype.OnlyPaintCheck;
            acase.version = pkg.version;
            acase.ID = Urlencode.encode(`${acase.type}_${acase.poles}_${pkg.name}_${page}`);
            acase.scheme = `rubik:/launch?type=appx&dist=${Urlencode.encode(srcUrl)}&path=${Urlencode.encode(page)}`;
            cases.push(acase);
            index++;
        }
        return Promise.resolve(cases);
    });
}
exports.build = build;
//# sourceMappingURL=tiny.js.map