import * as tslib_1 from "tslib";
import { PerspCameraSetup, CameraView, OrthoCameraSetup, CameraList } from "@intelligentgraphics/ig.gfx.support";
import { Controls } from "@intelligentgraphics/ig.gfx.threejs";
export var CAMERA_BACK_INDEX = 100;
export var CAMERA_FRONT_INDEX = 200;
export var createDefaultCameraSetup = function (_a) {
    var _b = _a.perspectives, perspectives = _b === void 0 ? ["3D"] : _b, _c = _a.initialPerspective, initialPerspective = _c === void 0 ? "3D" : _c, container = _a.container, cameraName2D = _a.cameraName2D, cameraName3D = _a.cameraName3D, viewFrame = _a.viewFrame, onControlChange = _a.onControlChange, remoteControls = _a.remoteControls;
    var e_1, _d;
    var createdSetups = new Map();
    try {
        for (var perspectives_1 = tslib_1.__values(perspectives), perspectives_1_1 = perspectives_1.next(); !perspectives_1_1.done; perspectives_1_1 = perspectives_1.next()) {
            var dimension = perspectives_1_1.value;
            if (createdSetups.has(dimension)) {
                throw new Error("A camera setup for " + dimension + " has already been defined.");
            }
            var setupFactory = dimension === "2D"
                ? createOrthographicCamera
                : createPerspectiveCamera;
            var name_1 = dimension === "2D" ? cameraName2D : cameraName3D;
            var view = setupFactory({
                sceneContainer: container,
                viewFrame: viewFrame,
                name: name_1,
                onControlChange: onControlChange,
                createControl: true,
                remoteControls: remoteControls
            }).view;
            CameraList.setCameraView(view);
            CameraList.enableViewControls(name_1, initialPerspective === dimension);
            CameraList.enableViewRendering(name_1, initialPerspective === dimension);
            createdSetups.set(dimension, view);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (perspectives_1_1 && !perspectives_1_1.done && (_d = perspectives_1.return)) _d.call(perspectives_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return createdSetups;
};
export var createPerspectiveCamera = function (_a) {
    var sceneContainer = _a.sceneContainer, viewFrame = _a.viewFrame, createControl = _a.createControl, onControlChange = _a.onControlChange, name = _a.name, remoteControls = _a.remoteControls;
    var setup = new PerspCameraSetup();
    var view = new CameraView({
        name: name,
        viewFrame: viewFrame,
        active: undefined,
        backgroundColor: undefined,
        zIndex: CAMERA_FRONT_INDEX
    }, setup);
    var control;
    if (createControl) {
        control = view.orbitControls = new Controls.OrbitControls(view.camera, sceneContainer, {
            remoteOnly: remoteControls
        });
        control.maxDistance = 20;
        if (onControlChange) {
            control.addEventListener("change", onControlChange);
        }
    }
    view.lookAt(setup.lookAt);
    return { setup: setup, view: view, control: control };
};
export var createOrthographicCamera = function (_a) {
    var sceneContainer = _a.sceneContainer, viewFrame = _a.viewFrame, createControl = _a.createControl, onControlChange = _a.onControlChange, name = _a.name, remoteControls = _a.remoteControls;
    var setup = new OrthoCameraSetup();
    var view = new CameraView({
        name: name,
        viewFrame: viewFrame,
        active: undefined,
        backgroundColor: undefined,
        zIndex: CAMERA_BACK_INDEX
    }, setup);
    var control;
    if (createControl) {
        control = view.orbitControls = new Controls.OrbitControls(view.camera, sceneContainer, {
            remoteOnly: remoteControls,
            radiusClamping: true
        });
        control.noRotate = true;
        control.touchCountMap.ORBIT = undefined;
        control.touchCountMap.PAN = 1;
        if (onControlChange) {
            control.addEventListener("change", onControlChange);
        }
    }
    return { setup: setup, view: view, control: control };
};
