"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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const path_resolver_1 = require("root/services/path-resolver");
const Dao_1 = __importDefault(require("root/models/Dao"));
const templates_1 = require("root/models/Model/templates");
const util_1 = require("root/services/util");
class Model {
    static get modelsJsonPath() {
        return path_1.default.join(path_resolver_1.getPrjRoot(), 'models.json');
    }
    static getList() {
        return __awaiter(this, void 0, void 0, function* () {
            const file = yield fs_1.promises.readFile(Model.modelsJsonPath);
            const jsonModels = JSON.parse(file.toString());
            const daos = yield Dao_1.default.getList();
            return jsonModels.map(jsonModel => {
                const model = new Model();
                model.id = jsonModel.id;
                model.name = jsonModel.name;
                model.dao = daos.find(d => d.id === jsonModel.daoId);
                return model;
            });
        });
    }
    static saveList(models) {
        return __awaiter(this, void 0, void 0, function* () {
            const jsonModels = models.map(m => {
                var _a, _b;
                return ({
                    id: m.id,
                    name: m.name,
                    daoId: (_b = (_a = m.dao) === null || _a === void 0 ? void 0 : _a.id, (_b !== null && _b !== void 0 ? _b : null)),
                });
            });
            yield fs_1.promises.writeFile(Model.modelsJsonPath, JSON.stringify(jsonModels, null, 2));
        });
    }
    create(dao, name) {
        return __awaiter(this, void 0, void 0, function* () {
            this.name = name;
            this.dao = dao;
            const models = yield Model.getList();
            this.id = (models
                .map(model => model.id)
                .reduce((max, curr) => Math.max(max, curr), 0) || 0) + 1;
            models.push(this);
            yield Model.saveList(models);
            const modelName = util_1.capitalize(name);
            if (!fs_1.default.existsSync(path_resolver_1.getModelsRoot())) {
                yield fs_1.promises.mkdir(path_resolver_1.getModelsRoot());
            }
            const folderPath = path_1.default.join(path_resolver_1.getModelsRoot(), modelName);
            yield fs_1.promises.mkdir(folderPath);
            if (!dao) {
                yield fs_1.promises.writeFile(path_1.default.join(folderPath, 'index.ts'), templates_1.indexTemplate(modelName));
            }
            else {
                yield fs_1.promises.writeFile(path_1.default.join(folderPath, 'index.ts'), dao.module.indexTemplate(modelName));
            }
            return true;
        });
    }
    toJSON() {
        var _a, _b;
        return Object.assign(Object.assign({}, this), { dao: (_b = (_a = this.dao) === null || _a === void 0 ? void 0 : _a.name, (_b !== null && _b !== void 0 ? _b : 'null')) });
    }
}
exports.default = Model;
//# sourceMappingURL=index.js.map