import { __values } from "tslib";
import { State } from "./State";
import { getViewNames, getView } from "./CameraViewUtils";
import Logger from "./Logger";
var HIDDEN_STATE_ENTRY = "globalIgnoreListForPostprocessing";
var globalIgnoreList = new Map();
function ensureGIL() {
    if (State[HIDDEN_STATE_ENTRY] == undefined) {
        State[HIDDEN_STATE_ENTRY] = globalIgnoreList; // hold list entries
        // globalIgnoreList = new Map<string, IGlobalIgnoreInPostprocessing>();
        // State[HIDDEN_STATE_ENTRY] = globalIgnoreList; // fresh list entries when State was resetted
    }
    return globalIgnoreList;
}
/**
 * sets object to be ignored in post processing for specific (or all) views
 * @param label
 * @param obj
 * @param cameraViewName
 */
export function setGlobalIgnore3DObjectInPostprocessing(label, obj, cameraViewName) {
    if (cameraViewName === void 0) { cameraViewName = "all"; }
    var gil = ensureGIL();
    if (!obj) {
        Logger.warn("setGlobalIgnore3DObjectInPostprocessing with null object: use removeGlobalIgnore3DObjectInPostprocessing instead");
    }
    var entry = {
        object: obj,
        cameraViewName: cameraViewName,
    };
    gil.set(label, entry);
    refreshIgnorePostprocessingInCameraViews();
}
/**
 * removes object to be ignored in post processing
 * @param label
 */
export function removeGlobalIgnore3DObjectInPostprocessing(label) {
    var gil = ensureGIL();
    gil.delete(label);
    refreshIgnorePostprocessingInCameraViews();
}
/**
 * updates the respectingIgnored3DObjects ignore list for all registered views
 */
export function refreshIgnorePostprocessingInCameraViews() {
    var e_1, _a;
    var allCVNames = getViewNames();
    var _loop_1 = function (cvName) {
        var cv = getView(cvName);
        if (cv &&
            cv.postprocessing &&
            cv.postprocessing.respectingIgnored3DObjects) {
            globalIgnoreList.forEach(function (entry, label) {
                if (entry.cameraViewName === "all" ||
                    entry.cameraViewName.indexOf(cvName) > -1) {
                    cv.postprocessing.respectingIgnored3DObjects.setIgnored3DObject(label, entry.object);
                }
            });
        }
    };
    try {
        for (var allCVNames_1 = __values(allCVNames), allCVNames_1_1 = allCVNames_1.next(); !allCVNames_1_1.done; allCVNames_1_1 = allCVNames_1.next()) {
            var cvName = allCVNames_1_1.value;
            _loop_1(cvName);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (allCVNames_1_1 && !allCVNames_1_1.done && (_a = allCVNames_1.return)) _a.call(allCVNames_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
}
export function getGlobalIgnored3DObjectsForCameraView(cameraViewName) {
    var result = new Array();
    globalIgnoreList.forEach(function (entry) {
        if (entry.cameraViewName === "all" ||
            entry.cameraViewName === cameraViewName) {
            result.push(entry.object);
        }
    });
    return result;
}
