"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PredicateApiImpl = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const StringExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/StringExtensions"));
const PredicateInternalApi_1 = require("../Internal/PredicateInternalApi");
const AdaptablePredicate_1 = require("../../AdaptableState/Common/AdaptablePredicate");
class PredicateApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new PredicateInternalApi_1.PredicateInternalApi(_adaptable);
    }
    getPredicateDefs() {
        return this.internalApi.mergeSystemAndCustomPredicates(this.getSystemPredicateDefs(), this.getCustomPredicateDefs());
    }
    getSystemPredicateDefs() {
        return AdaptablePredicate_1.SystemPredicateDefs;
    }
    getSystemPredicateDefsByModuleScope(moduleScope) {
        return AdaptablePredicate_1.SystemPredicateDefs.filter((p) => p.moduleScope.includes(moduleScope));
    }
    getCustomPredicateDefs() {
        return this.getPredicateOptions().customPredicateDefs;
    }
    getPredicateDefsByModuleScope(moduleScope) {
        return this.getPredicateDefs().filter((p) => p.moduleScope.includes(moduleScope));
    }
    getPredicateDefById(predicateId) {
        return this.getPredicateDefs().find((predicateDef) => predicateDef.id === predicateId);
    }
    getSystemPredicateDefById(predicateId) {
        return this.getSystemPredicateDefs().find((predicateDef) => predicateDef.id === predicateId);
    }
    getCustomPredicateDefById(predicateId) {
        return this.getCustomPredicateDefs().find((predicateDef) => predicateDef.id === predicateId);
    }
    predicateToString(predicate) {
        const predicateDef = this.getPredicateDefById(predicate.PredicateId);
        if (!predicateDef) {
            this.logWarn('Cannot find Predicate with Id:' + predicate.PredicateId);
            return '[Predicate Not found]';
        }
        let strPredicate = predicateDef.hasOwnProperty('toString')
            ? predicateDef.toString({ inputs: predicate.Inputs })
            : predicateDef.label;
        if ('ColumnId' in predicate) {
            strPredicate = `[${predicate.ColumnId}] ${strPredicate}`;
        }
        return strPredicate;
    }
    predicatesToString(predicates, logicalOperator = 'AND') {
        return predicates
            .map((predicate) => this.predicateToString(predicate))
            .join(` ${logicalOperator} `);
    }
    isValidPredicate(predicate) {
        if (!predicate) {
            return false;
        }
        const adaptablePredicateDef = this.getPredicateDefById(predicate.PredicateId);
        if (!adaptablePredicateDef) {
            return false;
        }
        // not perfect but if there are no inputs - and there should be some - we treat the filter as invalid
        if (adaptablePredicateDef.inputs) {
            if (ArrayExtensions_1.default.IsNullOrEmpty(predicate.Inputs) ||
                StringExtensions_1.default.IsNullOrEmpty(predicate.Inputs[0])) {
                return false;
            }
        }
        return !(['In', 'NotIn'].includes(predicate.PredicateId) &&
            ArrayExtensions_1.default.IsNullOrEmpty(predicate.Inputs));
    }
    isEveryPredicateValid(predicates) {
        return predicates.every((predicate) => this.isValidPredicate(predicate));
    }
    handleColumnPredicate(predicate, context, defaultReturn) {
        if (!predicate.ColumnId) {
            return this.handlePredicate(predicate, context, defaultReturn);
        }
        const paramsForColumn = {
            ...context,
            column: this.getColumnApi().getColumnWithColumnId(predicate.ColumnId),
            // value: params?.node?.data?.[predicate.ColumnId],
            value: this.getGridApi().getRawValueFromRowNode(context.node, predicate.ColumnId),
        };
        if ('oldValue' in paramsForColumn) {
            delete paramsForColumn.oldValue;
        }
        return this.handlePredicate(predicate, paramsForColumn, defaultReturn);
    }
    handleColumnPredicates(predicates, context, defaultReturn) {
        return predicates.every((predicate) => this.handleColumnPredicate(predicate, context, defaultReturn));
    }
    handlePredicate(predicate, context, defaultReturn) {
        if (!predicate) {
            return defaultReturn;
        }
        const predicateDef = this.getPredicateDefById(predicate.PredicateId);
        if (predicateDef === undefined) {
            return defaultReturn;
        }
        if (predicateDef.inputs?.some((_, i) => predicate.Inputs?.[i] === undefined || predicate.Inputs?.[i] === '')) {
            return defaultReturn;
        }
        try {
            return predicateDef.handler({
                adaptableApi: this.getAdaptableApi(),
                inputs: predicate.Inputs,
                ...context,
            });
        }
        catch (error) {
            console.error(`Error in predicate ${predicateDef.label}`, error);
            return false;
        }
    }
    handlePredicates(predicates, params, defaultReturn) {
        if (predicates === undefined || predicates === null || predicates?.length === 0) {
            return this.handlePredicate(undefined, params, defaultReturn);
        }
        if (params.predicatesOperator && params.predicatesOperator === 'OR') {
            return predicates?.some((p) => this.handlePredicate(p, params, defaultReturn));
        }
        return predicates?.every((p) => this.handlePredicate(p, params, defaultReturn));
    }
    useCaseSensitivity() {
        return this.getPredicateOptions().caseSensitivePredicates;
    }
}
exports.PredicateApiImpl = PredicateApiImpl;
