import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2023-2025 GoodData Corporation
import { memo, useCallback } from "react";
import { useIntl } from "react-intl";
import { DropdownControl } from "./DropdownControl.js";
import { messages } from "../../../locales.js";
import { orientationDropdownItems } from "../../constants/dropdowns.js";
import { getTranslatedDropdownItems } from "../../utils/translations.js";
export function convertXYNamePosition(namePosition) {
    if (!namePosition?.position) {
        return undefined;
    }
    const xAxisPosition = ["left", "center", "right"];
    const yAxisPosition = ["top", "middle", "bottom"];
    let currentPosition = xAxisPosition.indexOf(namePosition.position);
    if (currentPosition >= 0) {
        return { position: yAxisPosition[currentPosition] };
    }
    currentPosition = yAxisPosition.indexOf(namePosition.position);
    return { position: xAxisPosition[currentPosition] };
}
export function getAxesByChartOrientation(properties) {
    const { xaxis, yaxis } = properties?.controls || {};
    if (!xaxis && !yaxis) {
        return { xaxis, yaxis };
    }
    const resetProperties = {
        rotation: undefined,
        min: undefined,
        max: undefined,
        format: undefined,
    };
    const newXAxisName = convertXYNamePosition(yaxis?.name);
    const newYAxisName = convertXYNamePosition(xaxis?.name);
    return {
        xaxis: { ...resetProperties, ...yaxis, name: newXAxisName },
        yaxis: { ...resetProperties, ...xaxis, name: newYAxisName },
    };
}
export const OrientationDropdownControl = memo(function OrientationDropdownControl({ disabled, value, showDisabledMessage, properties, pushData, }) {
    const intl = useIntl();
    const handleOrientationChanged = useCallback((data) => {
        const { properties: dataProperties } = data;
        const isChanged = dataProperties?.controls?.["orientation"]?.position !==
            properties?.controls?.["orientation"]?.position;
        if (isChanged) {
            const { xaxis, yaxis } = getAxesByChartOrientation(dataProperties);
            const cloneProperties = {
                ...dataProperties,
                controls: {
                    ...dataProperties?.controls,
                    ...(xaxis ? { xaxis } : {}),
                    ...(yaxis ? { yaxis } : {}),
                },
            };
            pushData({ ...data, properties: cloneProperties });
        }
        else {
            pushData(data);
        }
    }, [properties, pushData]);
    const generateDropdownItems = useCallback(() => {
        return getTranslatedDropdownItems(orientationDropdownItems, intl);
    }, [intl]);
    return (_jsx(DropdownControl, { value: value, valuePath: "orientation.position", labelText: messages["orientationTitle"].id, disabled: disabled, properties: properties, pushData: handleOrientationChanged, items: generateDropdownItems(), showDisabledMessage: showDisabledMessage }));
});
//# sourceMappingURL=OrientationDropdownControl.js.map