"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
class Repository {
    constructor() {
        this.getOptionsWithArgs = (args) => {
            const { exportTo: Dto, user, paging } = args, options = __rest(args, ["exportTo", "user", "paging"]);
            const exportDto = new Dto();
            const _a = exportDto.middleware(undefined, user), { model: _ } = _a, queryOptions = __rest(_a, ["model"]);
            const page = parseInt(String(paging === null || paging === void 0 ? void 0 : paging.page), 10);
            const limit = parseInt(String(paging === null || paging === void 0 ? void 0 : paging.limit), 10);
            const pagingOptions = paging
                ? {
                    limit,
                    offset: limit * page,
                }
                : {};
            const additionalOptions = { distinct: true };
            const query = Object.assign(Object.assign(Object.assign(Object.assign({}, queryOptions), options), pagingOptions), additionalOptions);
            return query;
        };
        this.findAll = (model, args) => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const query = this.getOptionsWithArgs(args);
            if (args === null || args === void 0 ? void 0 : args.paging) {
                const { rows: raws, count: total } = yield model.findAndCountAll(query);
                const page = parseInt(String((_a = args === null || args === void 0 ? void 0 : args.paging) === null || _a === void 0 ? void 0 : _a.page), 10);
                const limit = parseInt(String((_b = args === null || args === void 0 ? void 0 : args.paging) === null || _b === void 0 ? void 0 : _b.limit), 10);
                const max_page = Math.ceil(total / limit) - 1;
                return {
                    data: raws,
                    page,
                    page_size: limit,
                    max_page,
                    has_prev: page !== 0,
                    has_next: max_page > page,
                    total_elements: total,
                };
            }
            const raws = yield model.findAll(query);
            return raws;
        });
        this.findOne = (model, args) => __awaiter(this, void 0, void 0, function* () {
            const query = this.getOptionsWithArgs(args);
            const raw = yield model.findOne(query);
            return raw;
        });
        this.repository = {
            findAll: this.findAll,
            findOne: this.findOne,
        };
    }
}
exports.default = Repository;
//# sourceMappingURL=index.js.map