"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 });
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const path_resolver_1 = require("root/services/path-resolver");
const templates_1 = require("root/models/Endpoint/templates");
const Route_1 = __importDefault(require("root/models/Route"));
var HttpMethod;
(function (HttpMethod) {
    HttpMethod["GET"] = "get";
    HttpMethod["POST"] = "post";
    HttpMethod["DELETE"] = "delete";
    HttpMethod["PUT"] = "put";
    HttpMethod["PATCH"] = "patch";
})(HttpMethod || (HttpMethod = {}));
exports.HttpMethod = HttpMethod;
class Endpoint {
    static get endpointsJsonPath() {
        return path_1.default.join(path_resolver_1.getPrjRoot(), 'endpoints.json');
    }
    static get functionsJsonPath() {
        return path_1.default.join(path_resolver_1.getPrjRoot(), 'functions.json');
    }
    static getList() {
        return __awaiter(this, void 0, void 0, function* () {
            const file = yield fs_1.promises.readFile(Endpoint.endpointsJsonPath);
            const endpoints = JSON.parse(file.toString());
            console.log(endpoints);
            return endpoints.map(endpoint => {
                const ep = new Endpoint();
                ep.id = endpoint.id;
                ep.route = Route_1.default.parseFromText(endpoint.route);
                ep.method = endpoint.method;
                return ep;
            });
        });
    }
    static saveList(endpoints) {
        return __awaiter(this, void 0, void 0, function* () {
            const jsonEndpoints = endpoints.map(ep => (Object.assign(Object.assign({}, ep), { route: ep.route.endpointRoute })));
            yield fs_1.promises.writeFile(Endpoint.endpointsJsonPath, JSON.stringify(jsonEndpoints, null, 2));
        });
    }
    create(route, method) {
        return __awaiter(this, void 0, void 0, function* () {
            this.route = route;
            this.method = method;
            const endpoints = yield Endpoint.getList();
            this.id = (endpoints
                .map(endpoint => endpoint.id)
                .reduce((max, curr) => Math.max(max, curr), 0) || 0) + 1;
            endpoints.push(this);
            yield Endpoint.saveList(endpoints);
            const folderPath = path_1.default.join(path_resolver_1.getEndpointsRoot(), this.method + '-' + route.folderName);
            yield fs_1.promises.mkdir(folderPath);
            yield fs_1.promises.writeFile(path_1.default.join(folderPath, 'index.ts'), templates_1.indexTemplate());
            yield fs_1.promises.writeFile(path_1.default.join(folderPath, 'handler.ts'), templates_1.handlerTemplate());
            yield fs_1.promises.writeFile(path_1.default.join(folderPath, 'interfaces.ts'), templates_1.interfacesTemplate());
            yield fs_1.promises.writeFile(path_1.default.join(path_resolver_1.getSrcRoot(), 'exporter.ts'), templates_1.exporterTemplate(endpoints));
            const functions = yield Endpoint.getFunctions();
            const functionName = this.method + route.functionName;
            functions[functionName] = {
                handler: `dist/exporter.${functionName}`,
                events: [
                    {
                        http: {
                            path: route.functionPath,
                            method: this.method,
                            cors: true,
                        },
                    },
                ],
            };
            yield Endpoint.saveFunctions(functions);
        });
    }
    static getFunctions() {
        return __awaiter(this, void 0, void 0, function* () {
            const file = yield fs_1.promises.readFile(Endpoint.functionsJsonPath);
            return JSON.parse(file.toString()).functions;
        });
    }
    static saveFunctions(functions) {
        return __awaiter(this, void 0, void 0, function* () {
            yield fs_1.promises.writeFile(Endpoint.functionsJsonPath, JSON.stringify({ functions }, null, 2));
        });
    }
}
exports.default = Endpoint;
//# sourceMappingURL=index.js.map