"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publish = exports.writeProjectData = exports.readProjectData = exports.initProject = exports.initProjects = exports.assetUrl = exports.assetsDir = exports.projectPath = void 0;
const promises_1 = require("node:fs/promises");
const path_1 = require("path");
const os_1 = require("os");
async function mkdirIfExists(path, options = null) {
    try {
        return await (0, promises_1.mkdir)(path, options);
    }
    catch (err) {
        if (err.code === 'EEXIST') {
            return;
        }
        else {
            throw err;
        }
    }
}
const FS_ROOT = process.env.FS_ROOT || (0, path_1.join)((0, os_1.homedir)(), '.silex');
const ASSETS_PATH = 'assets';
const ASSETS_URL = 'assets';
const HTML_PATH = '';
const CSS_PATH = '';
const PROJECT_FILE_NAME = '/.silex.data.json';
const PUBLICATION_PATH = 'publication';
function projectPath(projectId = 'default') {
    return (0, path_1.join)(FS_ROOT, projectId);
}
exports.projectPath = projectPath;
async function assetsDir(projectId) {
    return (0, path_1.join)(projectPath(projectId), ASSETS_PATH);
}
exports.assetsDir = assetsDir;
async function assetUrl(projectId, fileName) {
    return `${ASSETS_URL}/${fileName}`;
}
exports.assetUrl = assetUrl;
async function initProjects() {
    await mkdirIfExists(FS_ROOT);
}
exports.initProjects = initProjects;
async function initProject(projectId) {
    const projectFolder = projectPath(projectId);
    const uploadDir = (0, path_1.join)(projectFolder, ASSETS_PATH);
    await mkdirIfExists(uploadDir, { recursive: true, });
}
exports.initProject = initProject;
async function readProjectData(projectId) {
    return (0, promises_1.readFile)(projectPath(projectId) + PROJECT_FILE_NAME);
}
exports.readProjectData = readProjectData;
async function writeProjectData(projectId, data) {
    return (0, promises_1.writeFile)(projectPath(projectId) + PROJECT_FILE_NAME, JSON.stringify(data));
}
exports.writeProjectData = writeProjectData;
function required(prop, name) {
    if (!prop)
        throw new Error(`Error missing required param ${name}`);
}
async function publish(projectId, files, data) {
    const { assets, settings, publication } = data;
    required(assets, 'assets');
    required(files, 'files');
    required(projectId, 'projectId');
    const projectFolder = projectPath(projectId);
    const publicationDir = (0, path_1.join)(projectFolder, publication?.path || PUBLICATION_PATH);
    await Promise.all(files
        .map(async (file) => {
        try {
            const htmlPath = (0, path_1.join)(publicationDir, file.htmlPath);
            const cssPath = (0, path_1.join)(publicationDir, file.cssPath);
            await mkdirIfExists((0, path_1.dirname)(htmlPath), { recursive: true, });
            await mkdirIfExists((0, path_1.dirname)(cssPath), { recursive: true, });
            await (0, promises_1.writeFile)(htmlPath, file.html);
            await (0, promises_1.writeFile)(cssPath, file.css);
        }
        catch (err) {
            throw new Error(`Publication error: could not write files ${file.cssPath} and ${file.htmlPath}. ${err.message}`);
        }
    }));
    const assetsFolder = (0, path_1.join)(publicationDir, publication?.assets?.path || ASSETS_PATH);
    await mkdirIfExists(assetsFolder, { recursive: true, });
    assets.forEach(async (asset) => {
        required(asset.src, 'asset\'s "src" attribute');
        const src = (0, path_1.join)(projectFolder, asset.src);
        const dst = (0, path_1.join)(assetsFolder, (0, path_1.basename)(asset.src));
        try {
            await (0, promises_1.copyFile)(src, dst);
        }
        catch (err) {
            throw new Error(`Publication error: could not copy asset file ${src} to ${dst}. ${err.message}`);
        }
    });
    return publication?.url;
}
exports.publish = publish;
