"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fileUtils_1 = require("../common/fileUtils");
const Constants_1 = require("./Constants");
function copyStaticAssets(project, config) {
    copyDistAssets(config);
    copyStaticResources(project, config);
    copyContentAssets(project, config);
}
exports.copyStaticAssets = copyStaticAssets;
function copyDistAssets(config) {
    const distAssetsPath = path_1.default.join(config.serverDir, 'dist', 'assets');
    try {
        const localDevAssetsPath = path_1.default.join(config.buildDir, 'assets', 'localdev');
        fileUtils_1.removeFile(localDevAssetsPath);
        fileUtils_1.copyFiles(path_1.default.join(distAssetsPath, '*'), localDevAssetsPath);
    }
    catch (e) {
        throw new Error(`Unable to copy dist assets: ${e.message || e}`);
    }
}
exports.copyDistAssets = copyDistAssets;
function copyStaticResources(project, config) {
    const staticResources = project.staticResourcesDirectories;
    try {
        if (staticResources && staticResources.length > 0) {
            const assetsPath = path_1.default.join(config.buildDir, 'assets', 'project', Constants_1.STATIC_RESOURCES);
            fileUtils_1.removeFile(assetsPath);
            staticResources.forEach((item) => {
                fileUtils_1.copyFiles(path_1.default.join(item, '*'), assetsPath);
            });
        }
    }
    catch (e) {
        throw new Error(`Unable to copy static resources: ${e.message || e}`);
    }
}
exports.copyStaticResources = copyStaticResources;
function copyContentAssets(project, config) {
    const contentAssetsDir = project.contentAssetsDirectory;
    try {
        if (contentAssetsDir && contentAssetsDir !== '') {
            const assetsPath = path_1.default.join(config.buildDir, 'assets', 'project', Constants_1.CONTENT_ASSETS);
            fileUtils_1.removeFile(assetsPath);
            fileUtils_1.copyFiles(path_1.default.join(contentAssetsDir, '*'), assetsPath);
        }
    }
    catch (e) {
        console.warn(`Unable to copy contentAssets: ${e.getMessage || e}`);
    }
}
exports.copyContentAssets = copyContentAssets;
function rebuildResource(project, config, resourcePath) {
    if (isValidStaticResource(project, resourcePath)) {
        copyStaticResources(project, config);
    }
    else if (isValidContentAsset(project, resourcePath)) {
        copyContentAssets(project, config);
    }
    else {
        console.error(`Unable to reload resource ${resourcePath}` +
            `to the local dev server cache. This resource was not ` +
            `specified as part of the project's localdevserver.config.json.`);
        return;
    }
}
exports.rebuildResource = rebuildResource;
function isValidStaticResource(project, resourcePath) {
    let isValidStaticResource = false;
    const staticResources = project.staticResourcesDirectories;
    if (staticResources && staticResources.length > 0) {
        staticResources.forEach(item => {
            if (resourcePath.startsWith(item)) {
                isValidStaticResource = true;
                return;
            }
        });
    }
    return isValidStaticResource;
}
exports.isValidStaticResource = isValidStaticResource;
function isValidContentAsset(project, resourcePath) {
    const contentAssetsDir = project.contentAssetsDirectory;
    return (contentAssetsDir !== undefined &&
        contentAssetsDir !== '' &&
        resourcePath.startsWith(contentAssetsDir));
}
exports.isValidContentAsset = isValidContentAsset;
//# sourceMappingURL=StaticResourcesUtils.js.map