/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { QueryBuilderFilterOperator } from '../QueryBuilderFilterOperator.js';
import { Enumeration, PRIMITIVE_TYPE, } from '@finos/legend-graph';
import { hashArray } from '@finos/legend-shared';
import { buildFilterConditionState, buildFilterConditionExpression, getFilterExpressionLeftSideTypePath, } from './QueryBuilderFilterOperatorValueSpecificationBuilder.js';
import { QUERY_BUILDER_SUPPORTED_FUNCTIONS } from '../../../graph/QueryBuilderMetaModelConst.js';
import { buildNotExpression, getStandardPrimitiveTypeEquivalent, isTypeCompatibleForAssignment, unwrapNotExpression, } from '../../QueryBuilderValueSpecificationHelper.js';
import { QUERY_BUILDER_STATE_HASH_STRUCTURE } from '../../QueryBuilderStateHashUtils.js';
import { buildDefaultInstanceValue } from '../../shared/ValueSpecificationEditorHelper.js';
export class QueryBuilderFilterOperator_Equal extends QueryBuilderFilterOperator {
    getLabel() {
        return 'is';
    }
    isCompatibleWithFilterConditionProperty(filterConditionState) {
        const propertyType = filterConditionState.leftConditionType;
        const normalizedType = getStandardPrimitiveTypeEquivalent(propertyType);
        return ((normalizedType !== undefined &&
            [
                PRIMITIVE_TYPE.STRING,
                PRIMITIVE_TYPE.BOOLEAN,
                PRIMITIVE_TYPE.NUMBER,
                PRIMITIVE_TYPE.INTEGER,
                PRIMITIVE_TYPE.DECIMAL,
                PRIMITIVE_TYPE.FLOAT,
                PRIMITIVE_TYPE.DATE,
                PRIMITIVE_TYPE.STRICTDATE,
                PRIMITIVE_TYPE.DATETIME,
            ].includes(normalizedType)) ||
            // if the type is enumeration, make sure the enumeration has some value
            (propertyType instanceof Enumeration && propertyType.values.length > 0));
    }
    isCompatibleWithFilterConditionValue(filterConditionState) {
        return isTypeCompatibleForAssignment(filterConditionState.rightConditionValue &&
            !filterConditionState.rightConditionValue.isCollection
            ? filterConditionState.rightConditionValue.type
            : undefined, filterConditionState.leftConditionType);
    }
    getDefaultFilterConditionValue(filterConditionState) {
        const propertyType = filterConditionState.leftConditionType;
        return buildDefaultInstanceValue(filterConditionState.filterState.queryBuilderState.graphManagerState
            .graph, propertyType, filterConditionState.filterState.queryBuilderState.observerContext, filterConditionState.filterState.queryBuilderState
            .INTERNAL__enableInitializingDefaultSimpleExpressionValue);
    }
    buildFilterConditionExpression(filterConditionState, lambdaParameterName) {
        return buildFilterConditionExpression(filterConditionState, filterConditionState.leftConditionType.path === PRIMITIVE_TYPE.DATETIME &&
            filterConditionState.rightConditionValue?.type?.path !==
                PRIMITIVE_TYPE.DATETIME
            ? QUERY_BUILDER_SUPPORTED_FUNCTIONS.IS_ON_DAY
            : QUERY_BUILDER_SUPPORTED_FUNCTIONS.EQUAL, lambdaParameterName);
    }
    buildFilterConditionState(filterState, expression) {
        return buildFilterConditionState(filterState, expression, getFilterExpressionLeftSideTypePath(expression) ===
            PRIMITIVE_TYPE.DATETIME &&
            expression.parametersValues[1]?.genericType?.value.rawType.path !==
                PRIMITIVE_TYPE.DATETIME
            ? QUERY_BUILDER_SUPPORTED_FUNCTIONS.IS_ON_DAY
            : QUERY_BUILDER_SUPPORTED_FUNCTIONS.EQUAL, this);
    }
    get hashCode() {
        return hashArray([
            QUERY_BUILDER_STATE_HASH_STRUCTURE.FILTER_OPERATOR_EQUAL,
        ]);
    }
}
export class QueryBuilderFilterOperator_NotEqual extends QueryBuilderFilterOperator_Equal {
    getLabel() {
        return `is not`;
    }
    buildFilterConditionExpression(filterConditionState, lambdaParameterName) {
        return buildNotExpression(super.buildFilterConditionExpression(filterConditionState, lambdaParameterName));
    }
    buildFilterConditionState(filterState, expression) {
        const innerExpression = unwrapNotExpression(expression);
        return innerExpression
            ? super.buildFilterConditionState(filterState, innerExpression)
            : undefined;
    }
    get hashCode() {
        return hashArray([
            QUERY_BUILDER_STATE_HASH_STRUCTURE.FILTER_OPERATOR_NOT_EQUAL,
        ]);
    }
}
//# sourceMappingURL=QueryBuilderFilterOperator_Equal.js.map