"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.moveAssetsToDev = exports.createDevDirectory = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chokidar_1 = __importDefault(require("chokidar"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const ora_1 = __importDefault(require("ora"));
const _directorys_1 = __importDefault(require("../utils/_directorys"));
const _fsUtils_1 = require("../utils/_fsUtils");
// creates the initial dev directory for working.
const createDevDirectory = () => __awaiter(void 0, void 0, void 0, function* () {
    const spinner = (0, ora_1.default)('Creating working directory').start();
    const directoriesToMake = [
        _directorys_1.default.shopRoot,
        _directorys_1.default.productionRoot,
        _directorys_1.default.developmentRoot,
        _directorys_1.default.devRoot,
        _directorys_1.default.staticRoot,
        _directorys_1.default.scriptsRoot,
        _directorys_1.default.scriptsModuleRoot,
        _directorys_1.default.stylesRoot,
        _directorys_1.default.stylesBase,
        _directorys_1.default.stylesComponents,
        _directorys_1.default.stylesMixins,
        _directorys_1.default.stylesSections,
        _directorys_1.default.stylesTemplates,
        _directorys_1.default.stylesTypography,
        _directorys_1.default.stylesVariables,
        _directorys_1.default.fontsRoot,
        _directorys_1.default.imagesRoot,
        _directorys_1.default.layoutRoot,
        _directorys_1.default.templatesRoot,
        _directorys_1.default.customersRoot,
        _directorys_1.default.snippetsRoot,
        _directorys_1.default.sectionsRoot,
        _directorys_1.default.configRoot,
        _directorys_1.default.localesRoot,
    ];
    try {
        yield (0, _fsUtils_1.createRecursiveDirectory)(directoriesToMake);
        if ((yield (0, _fsUtils_1.checkWorkingDirectory)()) === true) {
            return spinner.succeed('Finished creating working directory');
        }
    }
    catch (err) {
        spinner.fail('Failed creating working directory, maybe it already exists');
        return console.error(err);
    }
});
exports.createDevDirectory = createDevDirectory;
/**
 * Moves assets to the correct directory in dev folder.
 * @param array
 * @param directory
 * @param type
 */
const moveAssets = (array, directory, type) => __awaiter(void 0, void 0, void 0, function* () {
    const spinner = (0, ora_1.default)(`Moving ${type} to working directory`).start();
    try {
        const files = yield (0, fast_glob_1.default)(array);
        for (const [index, file] of files.entries()) {
            yield (0, _fsUtils_1.moveFile)(file, directory);
            if (index === files.length - 1)
                spinner.succeed(`Finished moving ${type} to working directory`);
        }
    }
    catch (err) {
        spinner.fail(`Error moving ${type} to working directory`);
        return console.error(err);
    }
});
const moveAssetsToDev = () => __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a;
    const spinner = (0, ora_1.default)('Moving assets folder into new working directory').start();
    const watcher = chokidar_1.default.watch(path_1.default.resolve('./shop/src/assets'));
    watcher.on('unlink', () => __awaiter(void 0, void 0, void 0, function* () {
        try {
            const directory = yield fs_extra_1.default.readdir(path_1.default.resolve('./shop/src/assets'));
            if (directory.length === 0) {
                yield fs_extra_1.default.remove(path_1.default.resolve('./shop/src/assets'));
                spinner.succeed('Removed assets folder from working directory');
                yield watcher.close();
            }
        }
        catch (err) {
            spinner.fail('Error removing assets directory from working directory');
            yield watcher.close();
            return console.error(err);
        }
    }));
    watcher.on('error', () => watcher.close());
    try {
        const assets = [
            {
                type: 'images',
                root: _directorys_1.default.imagesRoot,
                assets: [
                    path_1.default.resolve('./shop/src/assets/*.jpg'),
                    path_1.default.resolve('./shop/src/assets/*.png'),
                    path_1.default.resolve('./shop/src/assets/*.gif'),
                    path_1.default.resolve('./shop/src/assets/*.webp'),
                    path_1.default.resolve('./shop/src/assets/*.svg'),
                    path_1.default.resolve('./shop/src/assets/*.svg.liquid'),
                ]
            },
            {
                type: 'fonts',
                root: _directorys_1.default.fontsRoot,
                assets: [
                    path_1.default.resolve('./shop/src/assets/*.otf'),
                    path_1.default.resolve('./shop/src/assets/*.ttf'),
                    path_1.default.resolve('./shop/src/assets/*.eot'),
                    path_1.default.resolve('./shop/src/assets/*.woff'),
                    path_1.default.resolve('./shop/src/assets/*.woff2'),
                    path_1.default.resolve('./shop/src/assets/*.txt'),
                    path_1.default.resolve('./shop/src/assets/*.txt.liquid')
                ],
            },
            {
                type: 'styles',
                root: _directorys_1.default.stylesRoot,
                assets: [
                    path_1.default.resolve('./shop/src/assets/*.css'),
                    path_1.default.resolve('./shop/src/assets/*.scss'),
                    path_1.default.resolve('./shop/src/assets/*.css.liquid'),
                    path_1.default.resolve('./shop/src/assets/*.scss.liquid'),
                ],
            },
            {
                type: 'scripts',
                root: _directorys_1.default.scriptsRoot,
                assets: [
                    path_1.default.resolve('./shop/src/assets/*.js'),
                    path_1.default.resolve('./shop/src/assets/*.js.liquid'),
                ],
            },
        ];
        try {
            for (var assets_1 = __asyncValues(assets), assets_1_1; assets_1_1 = yield assets_1.next(), !assets_1_1.done;) {
                const asset = assets_1_1.value;
                yield moveAssets(asset.assets, asset.root, asset.type);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (assets_1_1 && !assets_1_1.done && (_a = assets_1.return)) yield _a.call(assets_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    catch (err) {
        return console.error(err);
    }
});
exports.moveAssetsToDev = moveAssetsToDev;
