import { EventEmitter } from '@angular/core';
import { TranslationObserver } from './translation-observer';
import { TranslationDirectionType } from './translation-direction-type';
export class TranslationBehavior {
    constructor(translationModel, variableCommunicator, variableStore, translationDirection, location, signalRAppId) {
        this.doTranslation = new EventEmitter();
        if (translationDirection === TranslationDirectionType.Horizontal) {
            const horizontalTranslation = new TranslationObserver(translationModel, variableCommunicator, variableStore, signalRAppId);
            horizontalTranslation.translationValueChanged.subscribe(inputValue => {
                const currentLocation = this.getTranslationValue(translationModel, (+inputValue), location.x);
                this.doTranslation.emit(currentLocation);
            });
        }
        if (translationDirection === TranslationDirectionType.Vertical) {
            const verticalTranslation = new TranslationObserver(translationModel, variableCommunicator, variableStore, signalRAppId);
            verticalTranslation.translationValueChanged.subscribe(inputValue => {
                const currentLocation = this.getTranslationValue(translationModel, (+inputValue), location.y);
                this.doTranslation.emit(currentLocation);
            });
        }
    }
    getTranslationValue(translationModel, inputValue, locationValue) {
        let newLocation;
        if (translationModel.isProportion === true) {
            const proportion = (translationModel.maxCoordinate - translationModel.minCoordinate) /
                (translationModel.maxProportionScale - translationModel.minProportionScale);
            if (translationModel.isReverseProportion === true) {
                const actualLocation = locationValue + translationModel.maxCoordinate;
                if ((+inputValue) > translationModel.maxProportionScale) {
                    newLocation = locationValue + translationModel.minCoordinate;
                }
                else if ((+inputValue) < translationModel.minProportionScale) {
                    newLocation = actualLocation;
                }
                else {
                    newLocation = actualLocation - (+inputValue - translationModel.minProportionScale) * proportion;
                }
            }
            else {
                if ((+inputValue) > translationModel.maxProportionScale) {
                    newLocation = translationModel.maxCoordinate + locationValue;
                }
                else if ((+inputValue) < translationModel.minProportionScale) {
                    newLocation = translationModel.minCoordinate + locationValue;
                }
                else {
                    newLocation = locationValue + translationModel.minCoordinate +
                        (inputValue - translationModel.minProportionScale) * proportion;
                }
            }
        }
        else {
            newLocation = inputValue + locationValue;
        }
        if (newLocation > translationModel.maxCoordinate + locationValue) {
            newLocation = translationModel.maxCoordinate + locationValue;
        }
        if (newLocation < translationModel.minCoordinate + locationValue) {
            newLocation = translationModel.minCoordinate + locationValue;
        }
        return newLocation;
    }
}
