"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var clean_1 = require("./clean");
var SCRIPTS = common_1.constants.SCRIPTS, PATH = common_1.constants.PATH;
function init(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, _a, force, _b, prod, pkg, _c, flags, PKG, deps, devDeps;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    settings = args.settings, _a = args.force, force = _a === void 0 ? false : _a, _b = args.prod, prod = _b === void 0 ? false : _b;
                    pkg = settings.package;
                    if (args.reset) {
                        return [2, reset({ settings: settings })];
                    }
                    return [4, saveConfigJson({ settings: settings, prod: prod })];
                case 1:
                    _d.sent();
                    return [4, copyPackage({ settings: settings, prod: prod })];
                case 2:
                    _d.sent();
                    _c = !force;
                    if (!_c) return [3, 4];
                    return [4, isInitialized({ settings: settings })];
                case 3:
                    _c = (_d.sent());
                    _d.label = 4;
                case 4:
                    if (_c) {
                        return [2];
                    }
                    flags = settings.init;
                    if (!flags.scripts) return [3, 6];
                    return [4, pkg.addFields('scripts', SCRIPTS).save()];
                case 5:
                    _d.sent();
                    _d.label = 6;
                case 6:
                    if (!flags.deps) return [3, 10];
                    PKG = common_1.constants.PKG;
                    return [4, common_1.npm.getVersions(PKG.dependencies)];
                case 7:
                    deps = _d.sent();
                    return [4, common_1.npm.getVersions(PKG.devDependencies)];
                case 8:
                    devDeps = _d.sent();
                    return [4, pkg
                            .addFields('dependencies', deps, { force: true })
                            .addFields('devDependencies', devDeps, { force: true })
                            .save()];
                case 9:
                    _d.sent();
                    _d.label = 10;
                case 10:
                    if (!flags.files) return [3, 12];
                    return [4, common_1.tmpl
                            .create(PATH.TEMPLATES)
                            .use(common_1.tmpl.copyFile({ force: force }))
                            .execute()];
                case 11:
                    _d.sent();
                    _d.label = 12;
                case 12: return [2];
            }
        });
    });
}
exports.init = init;
function reset(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var pkg;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    pkg = args.settings.package;
                    pkg.removeFields('scripts', SCRIPTS, { exclude: 'postinstall' }).save();
                    return [4, common_1.tmpl
                            .create(PATH.TEMPLATES)
                            .use(common_1.tmpl.deleteFile())
                            .execute()];
                case 1:
                    _a.sent();
                    return [4, clean_1.clean({})];
                case 2:
                    _a.sent();
                    common_1.log.info('');
                    common_1.log.info('👋   The auto-generated files and scripts from `@uiharness/electron` have been removed.');
                    common_1.log.info("    Run `" + common_1.log.cyan('uiharness init') + "` to recreate them.");
                    common_1.log.info('');
                    return [2];
            }
        });
    });
}
function saveConfigJson(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var electron, port, out, data, CONFIG, path;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    electron = args.settings.electron;
                    port = electron.port;
                    out = electron.out(args.prod);
                    data = {
                        electron: {
                            port: port,
                            main: out.main.path,
                            renderer: out.renderer.path,
                        },
                    };
                    CONFIG = common_1.constants.PATH.CONFIG;
                    path = common_1.fsPath.join(CONFIG.DIR, CONFIG.FILE);
                    return [4, common_1.file.stringifyAndSave(path, data)];
                case 1:
                    _a.sent();
                    return [2, data];
            }
        });
    });
}
function copyPackage(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, prod, electron, out, pkg;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    settings = args.settings, prod = args.prod;
                    electron = settings.electron;
                    out = electron.out(prod);
                    pkg = common_1.npm.pkg('.').json;
                    pkg.main = common_1.fsPath.join('..', out.main.path);
                    return [4, common_1.file.stringifyAndSave(common_1.fsPath.resolve(PATH.PACKAGE), pkg)];
                case 1:
                    _a.sent();
                    return [2];
            }
        });
    });
}
function isInitialized(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, pkg, init, exists, _a, _b, scripts, hasAllScripts;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    settings = args.settings;
                    pkg = settings.package;
                    init = settings.init;
                    exists = function (path) { return common_1.fs.pathExists(common_1.fsPath.resolve(path)); };
                    _a = init.files;
                    if (!_a) return [3, 4];
                    return [4, exists('./src')];
                case 1:
                    _b = !(_c.sent());
                    if (_b) return [3, 3];
                    return [4, exists('./static')];
                case 2:
                    _b = !(_c.sent());
                    _c.label = 3;
                case 3:
                    _a = (_b);
                    _c.label = 4;
                case 4:
                    if (_a) {
                        return [2, false];
                    }
                    scripts = tslib_1.__assign({}, SCRIPTS);
                    delete scripts.postinstall;
                    hasAllScripts = Object.keys(scripts).every(function (key) { return pkg.scripts[key] === scripts[key]; });
                    return [2, hasAllScripts];
            }
        });
    });
}
//# sourceMappingURL=init.js.map