"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCartesianAxisFormatterConfig = void 0;
const field_1 = require("../../types/field");
const timeFrames_1 = require("../../types/timeFrames");
const formatting_1 = require("../../utils/formatting");
const timeFrames_2 = require("../../utils/timeFrames");
/**
 * Get the formatter config for a cartesian axis
 * @param axisItem - Axis item
 * @param longestLabelWidth - Longest label width
 * @param rotate - Rotate
 * @param defaultNameGap - Default name gap
 * @param show - Show
 * @param parameters - Parameter values for conditional formatting
 * @returns Axis config
 */
const getCartesianAxisFormatterConfig = ({ axisItem, longestLabelWidth, rotate, defaultNameGap, show, parameters, timezone, displayTimezone, }) => {
    // Remove axis labels, lines, and ticks if the axis is not shown
    // This is done to prevent the grid from disappearing when the axis is not shown
    if (!show) {
        return {
            axisLabel: undefined,
            axisLine: {
                show,
            },
            axisTick: {
                show,
            },
        };
    }
    const isTimestamp = (0, field_1.isField)(axisItem) &&
        (axisItem.type === field_1.DimensionType.DATE ||
            axisItem.type === field_1.DimensionType.TIMESTAMP);
    // Only apply axis formatting if the axis is NOT a date or timestamp
    const hasFormattingConfig = !isTimestamp && (0, formatting_1.hasFormatting)(axisItem);
    const axisMinInterval = (0, field_1.isDimension)(axisItem) &&
        axisItem.timeInterval &&
        (0, timeFrames_2.isTimeInterval)(axisItem.timeInterval) &&
        timeFrames_2.timeFrameConfigs[axisItem.timeInterval].getAxisMinInterval();
    const axisLabelFormatter = (0, field_1.isDimension)(axisItem) &&
        axisItem.timeInterval &&
        (0, timeFrames_2.isTimeInterval)(axisItem.timeInterval) &&
        timeFrames_2.timeFrameConfigs[axisItem.timeInterval].getAxisLabelFormatter();
    const axisConfig = {};
    if (axisItem && (hasFormattingConfig || axisMinInterval)) {
        axisConfig.axisLabel = {
            formatter: (value) => (0, formatting_1.formatItemValue)(axisItem, value, true, parameters, timezone, displayTimezone),
        };
        axisConfig.axisPointer = {
            label: {
                formatter: (value) => value &&
                    typeof value === 'object' &&
                    'value' in value &&
                    value.value !== undefined
                    ? (0, formatting_1.formatItemValue)(axisItem, value.value, true, parameters, timezone, displayTimezone)
                    : undefined,
            },
        };
    }
    else if (axisLabelFormatter) {
        axisConfig.axisLabel = {
            formatter: axisLabelFormatter,
            rich: {
                bold: {
                    fontWeight: 'bold',
                },
            },
        };
        axisConfig.axisPointer = {
            label: {
                formatter: (value) => value &&
                    typeof value === 'object' &&
                    'value' in value &&
                    value.value !== undefined
                    ? (0, formatting_1.formatItemValue)(axisItem, value.value, true, parameters, timezone, displayTimezone)
                    : undefined,
            },
        };
    }
    else if (axisItem !== undefined &&
        (0, field_1.isTableCalculation)(axisItem) &&
        axisItem.type === undefined) {
        axisConfig.axisLabel = {
            formatter: (value) => (0, formatting_1.formatItemValue)(axisItem, value, false, parameters, timezone),
        };
        axisConfig.axisPointer = {
            label: {
                formatter: (value) => value &&
                    typeof value === 'object' &&
                    'value' in value &&
                    value.value !== undefined
                    ? (0, formatting_1.formatItemValue)(axisItem, value.value, false, parameters, timezone)
                    : undefined,
            },
        };
    }
    else if (axisItem &&
        (0, field_1.isDimension)(axisItem) &&
        axisItem.timeInterval &&
        (0, timeFrames_2.isTimeInterval)(axisItem.timeInterval)) {
        // Some int numbers are converted to float by default on echarts
        // This is to ensure the value is correctly formatted on some types
        switch (axisItem.timeInterval) {
            case timeFrames_1.TimeFrames.WEEK_NUM:
            case timeFrames_1.TimeFrames.WEEK:
                axisConfig.axisLabel = {
                    formatter: (value) => (0, formatting_1.formatItemValue)(axisItem, value, true, parameters, timezone),
                };
                axisConfig.axisPointer = {
                    label: {
                        formatter: (value) => value &&
                            typeof value === 'object' &&
                            'value' in value &&
                            value.value !== undefined
                            ? (0, formatting_1.formatItemValue)(axisItem, value.value, true, parameters)
                            : undefined,
                    },
                };
                break;
            default:
        }
    }
    if (axisMinInterval) {
        axisConfig.minInterval = axisMinInterval;
    }
    axisConfig.nameGap = defaultNameGap || 0;
    if (rotate) {
        const rotateRadians = (rotate * Math.PI) / 180;
        const oppositeSide = (longestLabelWidth || 0) * Math.sin(rotateRadians);
        axisConfig.axisLabel = {
            ...(axisConfig.axisLabel || {}),
            rotate,
            margin: 12,
        };
        axisConfig.nameGap = oppositeSide + 15;
    }
    else {
        axisConfig.axisLabel = {
            ...(axisConfig.axisLabel || {}),
            hideOverlap: true,
        };
    }
    return axisConfig;
};
exports.getCartesianAxisFormatterConfig = getCartesianAxisFormatterConfig;
//# sourceMappingURL=getCartesianAxisFormatterConfig.js.map