"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeFile = exports.updateFile = exports.addFile = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const styleLinking_1 = __importDefault(require("../style-linking/styleLinking"));
// TODO: Probably need some sort of error handling in here at some stage.
// Need to see how well it works to begin with.
/**
 * Reusuable func to replace the file directory of a file.
 * @param file
 */
const replaceFileKey = (file) => {
    let key = file;
    key = key.replace(/src(.*?)/, 'dist');
    key = key.replace(/dev(.*?)/, 'assets');
    key = key.replace(/images(.*?)/, '');
    key = key.replace(/fonts(.*?)/, '');
    key = key.replace(/static(.*?)/, '');
    return key;
};
/**
 * Adds a file to the dist directory that themekit watches.
 * @param file
 */
const addFile = (file) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const fileKey = replaceFileKey(file);
        const source = yield fs_extra_1.default.readFile(file);
        yield fs_extra_1.default.writeFile(fileKey, source);
        yield (0, styleLinking_1.default)(fileKey);
        return;
    }
    catch (err) {
        return console.error(err);
    }
});
exports.addFile = addFile;
/**
 * Updates the file in the dist directory that themekit watches.
 * @param file
 */
const updateFile = (file) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const fileKey = replaceFileKey(file);
        const source = yield fs_extra_1.default.readFile(file);
        const target = yield fs_extra_1.default.readFile(fileKey);
        if (Buffer.compare(source, target) !== 0) {
            yield fs_extra_1.default.writeFile(fileKey, source);
            yield (0, styleLinking_1.default)(fileKey);
            return;
        }
        return;
    }
    catch (err) {
        return console.error(err);
    }
});
exports.updateFile = updateFile;
/**
 * Removes the file from the dist directory that themekit watches.
 * @param file - File src + name
 */
const removeFile = (file) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const fileKey = replaceFileKey(file);
        const isFileExists = yield fs_extra_1.default.pathExists(fileKey);
        if (isFileExists === true) {
            return yield fs_extra_1.default.remove(fileKey);
        }
        return;
    }
    catch (err) {
        return console.error(err);
    }
});
exports.removeFile = removeFile;
