import { getBuiltin, isArrayValue } from '@bilig/formula';
import { ErrorCode, ValueTag } from '@bilig/protocol';
import { errorValue } from '../../engine-value-utils.js';
const roundBuiltin = getBuiltin('ROUND');
const isEmptyCellGuardValue = (value) => value.tag === ValueTag.Empty || (value.tag === ValueTag.String && value.value === '');
const applyDirectCriteriaResultTransform = (readCellValueByIndex, transform, current) => {
    if (transform.kind === 'if-error') {
        return current.tag === ValueTag.Error ? transform.fallback : current;
    }
    if (transform.kind === 'if-empty-cell') {
        const guard = readCellValueByIndex(transform.cellIndex);
        if (guard.tag === ValueTag.Error) {
            return guard;
        }
        return isEmptyCellGuardValue(guard) ? transform.fallback : current;
    }
    const rounded = roundBuiltin?.(current, transform.digits) ?? errorValue(ErrorCode.Name);
    return isArrayValue(rounded) ? errorValue(ErrorCode.Value) : rounded;
};
const applyDirectCriteriaResultTransformsFrom = (readCellValueByIndex, formula, value, startIndex) => {
    const transforms = formula.directCriteria?.resultTransforms;
    if (!transforms || transforms.length === 0) {
        return value;
    }
    let current = value;
    for (let index = startIndex; index < transforms.length; index += 1) {
        current = applyDirectCriteriaResultTransform(readCellValueByIndex, transforms[index], current);
    }
    return current;
};
export const applyDirectCriteriaResultTransforms = (readCellValueByIndex, formula, value) => applyDirectCriteriaResultTransformsFrom(readCellValueByIndex, formula, value, 0);
export const tryEvaluateDirectCriteriaTransformShortCircuit = (readCellValueByIndex, formula) => {
    const transforms = formula.directCriteria?.resultTransforms;
    if (!transforms || transforms.length === 0) {
        return undefined;
    }
    for (let index = 0; index < transforms.length; index += 1) {
        const transform = transforms[index];
        if (transform.kind !== 'if-empty-cell') {
            continue;
        }
        const guard = readCellValueByIndex(transform.cellIndex);
        if (guard.tag === ValueTag.Error) {
            return applyDirectCriteriaResultTransformsFrom(readCellValueByIndex, formula, guard, index + 1);
        }
        if (isEmptyCellGuardValue(guard)) {
            return applyDirectCriteriaResultTransformsFrom(readCellValueByIndex, formula, transform.fallback, index + 1);
        }
    }
    return undefined;
};
const readColumnViewTag = (view, offset) => view.readTagAt(offset);
export const numericLikeValueInView = (view, offset) => {
    switch (readColumnViewTag(view, offset)) {
        case ValueTag.Number:
            return view.readNumberAt(offset);
        case ValueTag.Boolean:
            return view.readNumberAt(offset) !== 0 ? 1 : 0;
        case ValueTag.Empty:
            return 0;
        case ValueTag.String:
        case ValueTag.Error:
        default:
            return undefined;
    }
};
export const strictNumericAggregateCandidateInView = (view, offset) => readColumnViewTag(view, offset) === ValueTag.Number ? view.readNumberAt(offset) : undefined;
//# sourceMappingURL=formula-evaluation-direct-criteria-transforms.js.map