"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 __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVersion = exports.initializeWatchers = exports.downloadThemeFiles = exports.deployThemeFile = exports.createWorkingDirectory = void 0;
const _fsUtils_1 = require("./utils/_fsUtils");
const convert_1 = require("./convert/convert");
const deploy_1 = __importDefault(require("./deploy/deploy"));
exports.deployThemeFile = deploy_1.default;
const download_1 = __importDefault(require("./download/download"));
exports.downloadThemeFiles = download_1.default;
const liveReload_js_1 = require("./reload/liveReload.js");
const watch_1 = require("./watch/watch");
const version_1 = __importDefault(require("./version/version"));
exports.getVersion = version_1.default;
const _createPort_1 = __importDefault(require("./utils/_createPort"));
// TODO: Handle console.errors(), probably prettify output with
// chalk and a handler that takes in a callback etc - across all modules.
const createWorkingDirectory = () => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _fsUtils_1.cloneDirectory)();
    yield (0, convert_1.createDevDirectory)();
    yield (0, convert_1.moveAssetsToDev)();
});
exports.createWorkingDirectory = createWorkingDirectory;
const initializeWatchers = (environment) => __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a;
    const port = yield (0, _createPort_1.default)();
    const env = environment;
    const stepsToRun = [
        watch_1.initializeWebpack,
        watch_1.initializeWorkingDirectory,
    ];
    yield (0, watch_1.initializeThemekit)(port, env);
    yield (0, liveReload_js_1.removeLiveReload)();
    yield (0, liveReload_js_1.liveReload)(port);
    try {
        try {
            for (var stepsToRun_1 = __asyncValues(stepsToRun), stepsToRun_1_1; stepsToRun_1_1 = yield stepsToRun_1.next(), !stepsToRun_1_1.done;) {
                const step = stepsToRun_1_1.value;
                yield step();
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (stepsToRun_1_1 && !stepsToRun_1_1.done && (_a = stepsToRun_1.return)) yield _a.call(stepsToRun_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    catch (err) {
        console.error(err);
        return process.exit(1);
    }
});
exports.initializeWatchers = initializeWatchers;
