// (C) 2019-2025 GoodData Corporation
import { set } from "lodash-es";
import { bucketItems, bucketsFind, insightBuckets, insightProperties, } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { getMeasureGroupDimensionFromProperties } from "./propertiesHelper.js";
import { getTranslation } from "./translations.js";
import { messages } from "../../locales.js";
function fixEmptyMaxValue(value) {
    return value === "" ? Number.MAX_SAFE_INTEGER : Number(value);
}
function fixEmptyMinValue(value) {
    return value === "" ? Number.MIN_SAFE_INTEGER : Number(value);
}
function isValueMinusOrEmpty(value) {
    return value === "-" || value === "";
}
function isInvalidOrMinMaxError(value, minNumberValue, maxNumberValue) {
    const valueIsMinus = value === "-";
    const maxMinNumbers = !isNaN(minNumberValue) && !isNaN(maxNumberValue);
    return valueIsMinus || !maxMinNumbers || minNumberValue > maxNumberValue;
}
export function maxInputValidateAndPushData(data, state, props, setState, defaultState) {
    const { basePath } = props;
    const maxValue = data?.properties?.controls?.[basePath]?.max;
    const incorrectMinValue = state?.minScale?.incorrectValue ?? "";
    const correctMinValue = props?.properties?.controls?.[basePath]?.min ?? "";
    const incorrectMinInvalid = isValueMinusOrEmpty(incorrectMinValue);
    const minNumberValue = incorrectMinInvalid
        ? fixEmptyMinValue(correctMinValue)
        : Number(incorrectMinValue);
    const maxNumberValue = fixEmptyMaxValue(maxValue);
    const maxIsMinus = maxValue === "-";
    const { propertiesMeta, pushData } = props;
    set(propertiesMeta, "undoApplied", false);
    // dash, non-numeric or min/max mismatch: set error
    if (isInvalidOrMinMaxError(maxValue, minNumberValue, maxNumberValue)) {
        setState({
            minScale: {
                ...state.minScale,
                hasWarning: incorrectMinValue === "-",
            },
            maxScale: {
                hasWarning: true,
                // no error message for dash
                warningMessage: maxIsMinus ? "" : getTranslation(messages["axisMaxWarning"].id, props.intl),
                incorrectValue: maxValue,
            },
        });
        pushData?.({ propertiesMeta }); // post undoApplied flag to AD
        return;
    }
    // valid, set new value
    const { properties } = props;
    if (properties) {
        set(properties, `controls.${basePath}.max`, maxValue);
    }
    // if incorrect value was set previously but now validation passed, set incorrect value as correct value
    if (isNaN(parseFloat(incorrectMinValue))) {
        setState({
            maxScale: defaultState.maxScale,
        });
    }
    else {
        if (properties) {
            set(properties, `controls.${basePath}.min`, incorrectMinValue);
        }
        setState(defaultState);
    }
    pushData?.({ properties, propertiesMeta });
}
export function minInputValidateAndPushData(data, state, props, setState, defaultState) {
    const { basePath } = props;
    const minValue = data?.properties?.controls?.[basePath]?.min;
    const incorrectMaxValue = state?.maxScale?.incorrectValue ?? "";
    const correctMaxValue = props?.properties?.controls?.[basePath]?.max ?? "";
    const incorrectMaxInvalid = isValueMinusOrEmpty(incorrectMaxValue);
    const maxNumberValue = incorrectMaxInvalid
        ? fixEmptyMaxValue(correctMaxValue)
        : Number(incorrectMaxValue);
    const minNumberValue = fixEmptyMinValue(minValue);
    const minIsDash = minValue === "-";
    const { propertiesMeta, pushData } = props;
    set(propertiesMeta, "undoApplied", false);
    // dash, non-numeric or min/max mismatch: set error
    if (isInvalidOrMinMaxError(minValue, minNumberValue, maxNumberValue)) {
        setState({
            maxScale: {
                ...state.maxScale,
                hasWarning: incorrectMaxValue === "-",
            },
            minScale: {
                hasWarning: true,
                // no error message for dash
                warningMessage: minIsDash ? "" : getTranslation(messages["axisMinWarning"].id, props.intl),
                incorrectValue: minValue,
            },
        });
        pushData?.({ propertiesMeta }); // post undoApplied flag to AD
        return;
    }
    // valid, set new value
    const { properties } = props;
    if (properties) {
        set(properties, `controls.${basePath}.min`, minValue);
    }
    // if incorrect value was set previously but now validation passed, set incorrect value as correct value
    if (isNaN(parseFloat(incorrectMaxValue))) {
        setState({
            minScale: defaultState.minScale,
        });
    }
    else {
        if (properties) {
            set(properties, `controls.${basePath}.max`, incorrectMaxValue);
        }
        setState(defaultState);
    }
    pushData?.({ properties, propertiesMeta });
}
export function isSetColumnHeadersPositionToLeftAllowed(insight) {
    if (!insight) {
        return false;
    }
    const rowsBucket = bucketsFind(insightBuckets(insight), BucketNames.ATTRIBUTE);
    const hasRows = rowsBucket && bucketItems(rowsBucket).length > 0;
    const columnsBucket = bucketsFind(insightBuckets(insight), BucketNames.COLUMNS);
    const hasColumns = columnsBucket && bucketItems(columnsBucket).length > 0;
    const measureGroupDimension = getMeasureGroupDimensionFromProperties(insightProperties(insight)) ?? "columns";
    return hasColumns && !hasRows && measureGroupDimension === "rows";
}
//# sourceMappingURL=controlsHelper.js.map