"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertCoordinateFromRelativeValue = exports.convertCoordinateFromPixelValue = exports.convertCoordinateFromDataValue = exports.convertToRelativeCoordinate = exports.convertToPixelCoordinate = exports.convertToDataValue = exports.convertCoordinateMode = exports.convertFromAbsoluteCoordinate = exports.convertAbsoluteCoordinateToRelativeValue = exports.convertAbsoluteCoordinateToPixelValue = exports.convertAbsoluteCoordinateToDataValue = exports.convertToAbsoluteCoordinate = exports.convertRelativeValueToAbsoluteCoordinate = exports.convertPixelValueToAbsoluteCoordinate = exports.convertDataValueToAbsoluteCoordinate = exports.subtractCoord = exports.addCoord = void 0;
var ECoordinateMode_1 = require("../Charting/Visuals/Annotations/types/ECoordinateMode");
var translate_1 = require("../utils/translate");
// TODO additionally add undefined, null and NaN checks for converting functions
function assertParseValue(value) {
    if (value === value && value !== undefined && value !== null) {
        return value;
    }
    else {
        throw new Error("Unexpected value: ".concat(value, "!"));
    }
}
//#region Basic operations
function addCoord(a, b) {
    return (a + b);
}
exports.addCoord = addCoord;
function subtractCoord(a, b) {
    return (a - b);
}
exports.subtractCoord = subtractCoord;
//#endregion
//#region Convert to Absolute Coordinate
function convertDataValueToAbsoluteCoordinate(value, calc) {
    return calc.getCoordinate(value);
}
exports.convertDataValueToAbsoluteCoordinate = convertDataValueToAbsoluteCoordinate;
function convertPixelValueToAbsoluteCoordinate(value) {
    return (0, translate_1.translateToScaled)(value);
}
exports.convertPixelValueToAbsoluteCoordinate = convertPixelValueToAbsoluteCoordinate;
function convertRelativeValueToAbsoluteCoordinate(value, calc) {
    return (value * calc.viewportDimension);
}
exports.convertRelativeValueToAbsoluteCoordinate = convertRelativeValueToAbsoluteCoordinate;
function convertToAbsoluteCoordinate(value, calc, currentCoordinateMode) {
    switch (currentCoordinateMode) {
        case ECoordinateMode_1.ECoordinateMode.DataValue: {
            return convertDataValueToAbsoluteCoordinate(value, calc);
        }
        case ECoordinateMode_1.ECoordinateMode.Pixel: {
            return convertPixelValueToAbsoluteCoordinate(value);
        }
        case ECoordinateMode_1.ECoordinateMode.Relative: {
            return convertRelativeValueToAbsoluteCoordinate(value, calc);
        }
        default: {
            throw new Error("Unexpected coordinate mode: \"".concat(currentCoordinateMode, "\"!"));
        }
    }
}
exports.convertToAbsoluteCoordinate = convertToAbsoluteCoordinate;
//#endregion
//#region Convert from Absolute Coordinate
function convertAbsoluteCoordinateToDataValue(coord, calc) {
    return calc.getDataValue(coord);
}
exports.convertAbsoluteCoordinateToDataValue = convertAbsoluteCoordinateToDataValue;
function convertAbsoluteCoordinateToPixelValue(coord) {
    return (0, translate_1.translateToNotScaled)(coord);
}
exports.convertAbsoluteCoordinateToPixelValue = convertAbsoluteCoordinateToPixelValue;
function convertAbsoluteCoordinateToRelativeValue(coord, calc) {
    return (coord / calc.viewportDimension);
}
exports.convertAbsoluteCoordinateToRelativeValue = convertAbsoluteCoordinateToRelativeValue;
function convertFromAbsoluteCoordinate(coord, calc, targetCoordinateMode) {
    switch (targetCoordinateMode) {
        case ECoordinateMode_1.ECoordinateMode.DataValue: {
            return convertAbsoluteCoordinateToDataValue(coord, calc);
        }
        case ECoordinateMode_1.ECoordinateMode.Pixel: {
            return convertAbsoluteCoordinateToPixelValue(coord);
        }
        case ECoordinateMode_1.ECoordinateMode.Relative: {
            return convertAbsoluteCoordinateToRelativeValue(coord, calc);
        }
        default: {
            throw new Error("Unexpected coordinate mode: \"".concat(targetCoordinateMode, "\"!"));
        }
    }
}
exports.convertFromAbsoluteCoordinate = convertFromAbsoluteCoordinate;
//#endregion
function convertCoordinateMode(value, calc, currentCoordinateMode, targetCoordinateMode) {
    if (currentCoordinateMode === targetCoordinateMode) {
        return value;
    }
    var absoluteCoordinate = convertToAbsoluteCoordinate(value, calc, currentCoordinateMode);
    return convertFromAbsoluteCoordinate(absoluteCoordinate, calc, targetCoordinateMode);
}
exports.convertCoordinateMode = convertCoordinateMode;
//#region Convert to a specific Coordinate Mode
function convertToDataValue(value, calc, currentCoordinateMode) {
    return convertCoordinateMode(value, calc, currentCoordinateMode, ECoordinateMode_1.ECoordinateMode.DataValue);
}
exports.convertToDataValue = convertToDataValue;
function convertToPixelCoordinate(value, calc, currentCoordinateMode) {
    return convertCoordinateMode(value, calc, currentCoordinateMode, ECoordinateMode_1.ECoordinateMode.Pixel);
}
exports.convertToPixelCoordinate = convertToPixelCoordinate;
function convertToRelativeCoordinate(value, calc, currentCoordinateMode) {
    return convertCoordinateMode(value, calc, currentCoordinateMode, ECoordinateMode_1.ECoordinateMode.Relative);
}
exports.convertToRelativeCoordinate = convertToRelativeCoordinate;
//#endregion
//#region Convert from a specific Coordinate Mode
function convertCoordinateFromDataValue(value, calc, targetCoordinateMode) {
    return convertCoordinateMode(value, calc, ECoordinateMode_1.ECoordinateMode.DataValue, targetCoordinateMode);
}
exports.convertCoordinateFromDataValue = convertCoordinateFromDataValue;
function convertCoordinateFromPixelValue(value, calc, targetCoordinateMode) {
    return convertCoordinateMode(value, calc, ECoordinateMode_1.ECoordinateMode.Pixel, targetCoordinateMode);
}
exports.convertCoordinateFromPixelValue = convertCoordinateFromPixelValue;
function convertCoordinateFromRelativeValue(value, calc, targetCoordinateMode) {
    return convertCoordinateMode(value, calc, ECoordinateMode_1.ECoordinateMode.Relative, targetCoordinateMode);
}
exports.convertCoordinateFromRelativeValue = convertCoordinateFromRelativeValue;
//#endregion
