"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.removeLiveReload = exports.liveReload = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
// Function to remove the live reload from the layout files.
const removeLiveReload = () => __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a;
    const regexString = /<!-- Start Live Server Reloading -->([\s\S]*?)<!-- End Live Server Reloading -->/g;
    try {
        const files = yield fs_extra_1.default.readdir(path_1.default.resolve('./shop/src/layout'));
        try {
            for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield files_1.next(), !files_1_1.done;) {
                const file = files_1_1.value;
                const fileData = yield fs_extra_1.default.readFile(`./shop/src/layout/${file}`);
                if (fileData.toString().match(regexString)) {
                    const dataToWrite = fileData.toString().replace(regexString, '');
                    yield fs_extra_1.default.writeFile(path_1.default.resolve(`./shop/src/layout/${file}`), dataToWrite);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield _a.call(files_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    catch (err) {
        return console.error(err);
    }
});
exports.removeLiveReload = removeLiveReload;
// Function to add the websocket/live reload functionality to layout files.
const liveReload = (port) => __awaiter(void 0, void 0, void 0, function* () {
    var e_2, _b;
    const websocketPort = `ws://localhost:${port}`;
    try {
        const files = yield fs_extra_1.default.readdir(path_1.default.resolve('./shop/src/layout'));
        try {
            for (var files_2 = __asyncValues(files), files_2_1; files_2_1 = yield files_2.next(), !files_2_1.done;) {
                const file = files_2_1.value;
                const fileData = yield fs_extra_1.default.readFile(path_1.default.resolve(`./shop/src/layout/${file}`));
                if (fileData.toString().includes('<!-- Start Live Server Reloading -->') === false) {
                    const fileArray = fileData.toString().split('</body>');
                    const dataToWrite = `${fileArray[0]}
          <!-- Start Live Server Reloading -->
          <script>
            var ws = new WebSocket('${websocketPort}');
            ws.onmessage = function(evt) { location.reload(); }
          </script>
          <!-- End Live Server Reloading -->
        </body>
        ${fileArray[1]}`;
                    yield fs_extra_1.default.writeFile(path_1.default.resolve(`./shop/src/layout/${file}`), dataToWrite);
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (files_2_1 && !files_2_1.done && (_b = files_2.return)) yield _b.call(files_2);
            }
            finally { if (e_2) throw e_2.error; }
        }
    }
    catch (err) {
        return console.error(err);
    }
});
exports.liveReload = liveReload;
