"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PseudoForeignKeySelectionProvider = void 0;
const list_of_values_service_1 = require("../list-of-values.service");
const selection_provider_decorator_1 = require("../../decorators/selection-provider.decorator");
const common_1 = require("@nestjs/common");
const basic_filters_dto_1 = require("../../dtos/basic-filters.dto");
const solid_introspect_service_1 = require("../solid-introspect.service");
const string_helper_1 = require("../../helpers/string.helper");
const DEFAULT_LIMIT = 100;
let PseudoForeignKeySelectionProvider = class PseudoForeignKeySelectionProvider {
    constructor(listOfValuesService, introspectService) {
        this.listOfValuesService = listOfValuesService;
        this.introspectService = introspectService;
    }
    name() {
        return 'PseudoForeignKeySelectionProvider';
    }
    help() {
        return "# Use this provider to implement pseudo foreign key style relationships.";
    }
    async value(optionValue, ctxt) {
        const basicFilterQuery = new basic_filters_dto_1.BasicFilterDto(DEFAULT_LIMIT, 0);
        basicFilterQuery.filters = {
            [ctxt.valueFieldName]: {
                $eq: optionValue
            }
        };
        const modelServiceInstanceWrapper = this.introspectService.getProvider(`${(0, string_helper_1.classify)(ctxt.modelName)}Service`);
        if (!modelServiceInstanceWrapper) {
            throw new common_1.BadRequestException(`Invalid model name (${ctxt.modelName}) specified in PseudoForeignKeySelectionProvider context.`);
        }
        const modelService = modelServiceInstanceWrapper.instance;
        const recs = await modelService.find(basicFilterQuery);
        if (!recs.records || recs.records.length == 0) {
            throw new Error(`Invalid optionValue=${optionValue}, for PseudoForeignKeySelectionProvider with context: ${JSON.stringify(ctxt)}`);
        }
        return {
            label: recs.records[0][ctxt.labelFieldName],
            value: recs.records[0][ctxt.valueFieldName]
        };
    }
    async values(query, ctxt) {
        const basicFilterQuery = new basic_filters_dto_1.BasicFilterDto(ctxt.limit, ctxt.offset);
        if (query) {
            basicFilterQuery.filters = {
                $or: ctxt.whereClauseFields.map((whereClauseField) => {
                    return {
                        [whereClauseField]: {
                            $containsi: `%${query}%`
                        }
                    };
                })
            };
        }
        const modelServiceInstanceWrapper = this.introspectService.getProvider(`${(0, string_helper_1.classify)(ctxt.modelName)}Service`);
        if (!modelServiceInstanceWrapper) {
            throw new common_1.BadRequestException(`Invalid model name (${ctxt.modelName}) specified in PseudoForeignKeySelectionProvider context.`);
        }
        const modelService = modelServiceInstanceWrapper.instance;
        const recs = await modelService.find(basicFilterQuery);
        const selectionValues = recs.records.map(rec => {
            return {
                label: rec[ctxt.labelFieldName],
                value: rec[ctxt.valueFieldName]
            };
        });
        return selectionValues;
    }
};
exports.PseudoForeignKeySelectionProvider = PseudoForeignKeySelectionProvider;
exports.PseudoForeignKeySelectionProvider = PseudoForeignKeySelectionProvider = __decorate([
    (0, selection_provider_decorator_1.SelectionProvider)(),
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [list_of_values_service_1.ListOfValuesService,
        solid_introspect_service_1.SolidIntrospectService])
], PseudoForeignKeySelectionProvider);
//# sourceMappingURL=pseudo-foreign-key-selection-provider.service.js.map