"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 ComponentIndex_1 = __importDefault(require("../../common/ComponentIndex"));
const fs_1 = __importDefault(require("fs"));
const ALLOWED_SHOW_EXTENSIONS = {
    '.html': true,
    '.css': true,
    '.js': true
};
function projectMetadata(sessionNonce, project) {
    return {
        extendApp: ({ app }) => {
            app.get(`/localdev/${sessionNonce}/localdev.js`, (req, res, next) => {
                const componentIndex = new ComponentIndex_1.default(project);
                const json = componentIndex.getProjectMetadata();
                const localDevConfig = {
                    project: json
                };
                res.type('js');
                res.send(`window.LocalDev = ${JSON.stringify(localDevConfig)};`);
            });
            app.get(`/localdev/${sessionNonce}/errorDetails`, (req, res, next) => {
                const specifier = req.query.specifier;
                const specifierRegex = RegExp('c/[a-zA-Z]*', 'g');
                if (!specifierRegex.test(specifier)) {
                    res.json({
                        errors: [
                            {
                                message: 'The component specifier format is incorrect'
                            }
                        ]
                    });
                    return;
                }
                const normalizedFile = path_1.default.join(project.directory, '.localdevserver', 'webruntime', 'custom-component', 'dev', 'en-US', `${specifier}.js`);
                if (!fs_1.default.existsSync(normalizedFile)) {
                    res.json({
                        errors: [
                            { message: 'Could not find compiled component' }
                        ]
                    });
                    return;
                }
                const content = fs_1.default.readFileSync(normalizedFile);
                const stringContent = content.toString();
                res.json(JSON.parse(stringContent));
            });
        }
    };
}
exports.projectMetadata = projectMetadata;
//# sourceMappingURL=projectMetadata.js.map