"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressionInternalApi = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("../Implementation/ApiBase");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
class ExpressionInternalApi extends ApiBase_1.ApiBase {
    /**
     * Checks if a column is referenced in a given Expression
     * @param columnId Column to Check
     * @param expression Expression to Lookup
     */
    isColumnReferencedInExpression(columnId, expression) {
        const colIds = this.getExpressionApi().getColumnsFromExpression(expression);
        return colIds.some((c) => c === columnId);
    }
    evaluateExpressionInAdaptableQL(module, object, expression) {
        const evaluateExpressionExternallyFn = this.getExpressionOptions()?.evaluateAdaptableQLExternally;
        if (typeof evaluateExpressionExternallyFn !== 'function') {
            return true;
        }
        const context = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            module,
        };
        if (object) {
            context.object = object;
        }
        if (expression) {
            context.expression = expression;
            context.referencedColumns = this.getExpressionApi()
                .getColumnsFromExpression(expression)
                .map((columnId) => this.getColumnApi().getColumnWithColumnId(columnId));
        }
        return !evaluateExpressionExternallyFn(context);
    }
    shouldEvaluatePredicatesInAdaptableQL(module, object, predicates) {
        const evaluateExpressionExternallyFn = this.getExpressionOptions()?.evaluateAdaptableQLExternally;
        if (typeof evaluateExpressionExternallyFn !== 'function') {
            return true;
        }
        const context = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            module,
        };
        if (object) {
            context.object = object;
        }
        if (predicates) {
            context.predicates = predicates;
            // need to find a way to get the columns in the predicates
            // cand do later
            //        this.getAdaptableApi().columnScopeApi.getColumnsInScope();
        }
        return !evaluateExpressionExternallyFn(context);
    }
    getFieldsFromOptions() {
        const fields = this.getExpressionOptions().fields;
        if (fields != null && typeof fields === 'function') {
            return fields(this.getAdaptableInternalApi().buildBaseContext());
        }
        else {
            let arr = fields;
            if (arr && ArrayExtensions_1.default.IsNotNullOrEmpty(arr)) {
                return arr;
            }
        }
    }
    getAvailableFields(data) {
        const optionFields = (this.getFieldsFromOptions() ?? [])?.map((field) => ({
            ...field,
            label: field.label || field.name,
        }));
        // const existingFields = new Set<string>();
        // Exclude fields already in options
        // optionFields.forEach((field) => existingFields.add(field.value));
        // const queriableColumns = this.getColumnApi().getQueryableColumns();
        // Exclude queriable columns
        // queriableColumns.forEach((column) => existingFields.add(column.field));
        // const dataFields = this.getFieldsFromData(data).reduce<AdaptableField[]>((acc, field) => {
        //   if (!existingFields.has(field)) {
        //     acc.push({
        //       label: field,
        //       value: field,
        //     });
        //   }
        //   return acc;
        // }, []);
        return optionFields;
    }
    getFieldType(field) {
        const fieldDef = this.getFieldsFromOptions()?.find((f) => f.name === field);
        if (fieldDef) {
            return fieldDef.dataType;
        }
        return 'text';
    }
    getFieldsOfType(type) {
        if (!type) {
            return this.getAvailableFields() ?? [];
        }
        return this.getAvailableFields()?.filter((f) => f.dataType === type) ?? [];
    }
}
exports.ExpressionInternalApi = ExpressionInternalApi;
