"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var State_1 = require("./State");
/** Sorts the items in State.cameras list by their z_index property */
function sortViews(a, b) {
    if (a.settings.zIndex < b.settings.zIndex) {
        return -1;
    }
    else if (a.settings.zIndex > b.settings.zIndex) {
        return 1;
    }
    else {
        return 0;
    }
}
/**
 * Adds a new view to the Core.State.cameraViews list
 * @param view the CameraView to be added. If already exists it will update the existing view
 */
function setCameraView(view) {
    if (!view.name) {
        console.warn("CameraList.setCameraView: no view name given");
        return;
    }
    var tFound = false;
    for (var i = 0; i < State_1.State.cameraViews.length; i++) {
        var cv = State_1.State.cameraViews[i];
        if (cv && cv.name === view.name) {
            State_1.State.cameraViews[i] = view;
            tFound = true;
        }
    }
    if (!tFound) {
        State_1.State.cameraViews.push(view);
    }
    State_1.State.cameraViews.sort(sortViews);
    State_1.State.API.RedrawScene();
}
exports.setCameraView = setCameraView;
/**
 * The list of names of registered views
 */
function getViewNames() {
    var e_1, _a;
    var result = new Array();
    try {
        for (var _b = tslib_1.__values(State_1.State.cameraViews), _c = _b.next(); !_c.done; _c = _b.next()) {
            var view = _c.value;
            result.push(view.name);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return result;
}
exports.getViewNames = getViewNames;
/**
 * Gets a view by its name from ThreeJS.cameras list
 * @returns null if the name wasn't registered, otherwise the CameraView
 */
function getView(name) {
    var e_2, _a;
    try {
        for (var _b = tslib_1.__values(State_1.State.cameraViews), _c = _b.next(); !_c.done; _c = _b.next()) {
            var view = _c.value;
            if (view.name === name) {
                return view;
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return null;
}
exports.getView = getView;
/** Enable or disable the camera rendering, note the controls have to be enabled+disabled seperately */
function enableViewRendering(name, enable) {
    var tView = getView(name);
    if (tView) {
        tView.settings.active = enable;
    }
}
exports.enableViewRendering = enableViewRendering;
/** Update the view rectangle */
function updateViewFrame(name, viewFrame) {
    var cv = getView(name);
    if (cv) {
        cv.setFrame(viewFrame);
    }
}
exports.updateViewFrame = updateViewFrame;
/** Enable or disable the camera controls */
function enableViewControls(name, enable) {
    var tView = getView(name);
    if (tView && tView.orbitControls) {
        if (!enable) {
            try {
                //console.log("try onMouseUp "+name);
                tView.orbitControls.mouseUp(new MouseEvent("mouseup")); //event not used
            }
            catch (e) {
                // console.log('onMouseUpX() is not a part of this orbitcontrol');
                console.log(e);
                //console.log(tView);
            }
        }
        tView.orbitControls.enabled = enable;
    }
}
exports.enableViewControls = enableViewControls;
