/**
 * 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 { guaranteeType, UnsupportedOperationError, hashArray, } from '@finos/legend-shared';
import { AbstractPropertyExpression, INTERNAL__UnknownValueSpecification, LambdaFunctionInstanceValue, PRIMITIVE_TYPE, } from '@finos/legend-graph';
import { QueryBuilderSimpleProjectionColumnState, QueryBuilderDerivationProjectionColumnState, } from '../projection/QueryBuilderProjectionColumnState.js';
import { action, computed, makeObservable, observable } from 'mobx';
import { DEFAULT_LAMBDA_VARIABLE_NAME } from '../../../QueryBuilderConfig.js';
import { buildCalendarFunctionExpression, updateAggregateColumnState, } from './QueryBuilderAggregateCalendarFunctionValueSpecificationBuilder.js';
export class QueryBuilderAggregateCalendarFunction {
    dateColumn;
    calendarType;
    endDate;
    lambdaParameterName = DEFAULT_LAMBDA_VARIABLE_NAME;
    func;
    label;
    hash;
    constructor(func, label, hash) {
        makeObservable(this, {
            dateColumn: observable,
            lambdaParameterName: observable,
            calendarType: observable,
            endDate: observable,
            setDateColumn: action,
            setCalendarType: action,
            setLambdaParameterName: action,
            setEndDate: action,
            hashCode: computed,
        });
        this.func = func;
        this.label = label;
        this.hash = hash;
    }
    getLabel() {
        return this.label;
    }
    isCompatibleWithColumn(projectionColumnState) {
        if (projectionColumnState instanceof QueryBuilderSimpleProjectionColumnState) {
            const propertyType = projectionColumnState.propertyExpressionState.propertyExpression.func
                .value.genericType.value.rawType;
            return [
                PRIMITIVE_TYPE.NUMBER,
                PRIMITIVE_TYPE.INTEGER,
                PRIMITIVE_TYPE.DECIMAL,
                PRIMITIVE_TYPE.FLOAT,
            ].includes(propertyType.path);
        }
        return true;
    }
    buildCalendarFunctionExpression(p) {
        return buildCalendarFunctionExpression(this.func, this.dateColumn, this.calendarType, this.endDate, p);
    }
    updateAggregateColumnState(expression, aggregationColumnState) {
        updateAggregateColumnState(expression, this.func, this, aggregationColumnState);
    }
    buildCalendarFunctionExpressionFromState(aggregateColumnState, columnLambda) {
        let targetColumn;
        if (aggregateColumnState.projectionColumnState instanceof
            QueryBuilderSimpleProjectionColumnState &&
            columnLambda instanceof LambdaFunctionInstanceValue) {
            targetColumn = guaranteeType(columnLambda.values[0]?.expressionSequence[0], AbstractPropertyExpression);
        }
        else if (aggregateColumnState.projectionColumnState instanceof
            QueryBuilderDerivationProjectionColumnState) {
            targetColumn = guaranteeType(columnLambda, INTERNAL__UnknownValueSpecification);
        }
        if (!targetColumn) {
            throw new UnsupportedOperationError("Can't build calendar aggregation column: target column should be defined");
        }
        return this.buildCalendarFunctionExpression(targetColumn);
    }
    setDateColumn(val) {
        this.dateColumn = val;
    }
    setLambdaParameterName(val) {
        this.lambdaParameterName = val;
    }
    setCalendarType(val) {
        this.calendarType = val;
    }
    setEndDate(val) {
        this.endDate = val;
    }
    get hashCode() {
        return hashArray([
            this.hash,
            this.dateColumn ?? '',
            this.calendarType,
            this.endDate,
        ]);
    }
}
//# sourceMappingURL=QueryBuilderAggregateCalendarFunction.js.map