import { __values } from "tslib";
import { Vector, createBounds } from "./Math/";
import { State } from "./State";
/**
 * Intersects the root objects in the scene with an orthogonal box
 * and returns all objects that are fully or partially included.
 *
 * Note, currently the z coordinates are completely ignored!
 *
 * @param llb Left lower back point.
 * @param ruf Right upper front point.
 * @param partial Specifies if objects need to be partially (true) or fully (false) included.
 *
 * @returns List of found objects, resp. their names
 */
export function IntersectSceneWithBox(llb, ruf, partial) {
    var tHits = new Array();
    var tDoBoxesIntersect = function (rectBB, objBB) {
        if (rectBB.max.x <= objBB.min.x) {
            return false;
        }
        if (rectBB.min.x >= objBB.max.x) {
            return false;
        }
        if (rectBB.max.y <= objBB.min.y) {
            return false;
        }
        if (rectBB.min.y >= objBB.max.y) {
            return false;
        }
        return true;
    };
    var tDoBoxesOverlay = function (rectBB, objBB) {
        return (rectBB.min.x <= objBB.min.x &&
            rectBB.max.x >= objBB.max.x &&
            rectBB.min.y <= objBB.min.y &&
            rectBB.max.y >= objBB.max.y);
    };
    var tRectBounds = createBounds(Vector.CreateVec3(llb.x, llb.y, 0), Vector.CreateVec3(ruf.x, ruf.y, 0));
    State.API.GetRoots().forEach(function (root) {
        var tObjBounds = State.API.GetBounds(root, "World");
        if (partial) {
            if (tDoBoxesIntersect(tRectBounds, tObjBounds)) {
                tHits.push(root);
            }
        }
        else {
            if (tDoBoxesOverlay(tRectBounds, tObjBounds)) {
                tHits.push(root);
            }
        }
    });
    return tHits;
}
/**
 * checks is WebGL Context is available
 * https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/By_example/Detect_WebGL
 */
export function detectWebGL() {
    try {
        var canvas = document.createElement("canvas");
        // Get WebGLRenderingContext from canvas element.
        var gl = canvas.getContext("webgl") ||
            canvas.getContext("experimental-webgl");
        // Report the result.
        if (gl && gl instanceof WebGLRenderingContext) {
            return true;
        }
    }
    catch (e) { }
    return false;
}
/**
 * Create RegExp for key comparison, fixes keys containing "*" and/or "?"
 * At the toplevel | can be used to create OR
 * ab?c|de*f  -->  match for abXc  ;  def ; deXf ; deXXXf ; abxc etc
 *
 *
 * @param key IGXC snappoint key or foreign, * matches any string and ? matches a single character
 *
 */
export function createSaneRegExp(key) {
    var e_1, _a;
    if (!State.regexCache) {
        State.regexCache = {};
    }
    if (State.regexCache[key]) {
        //console.log("cached RE");
        return State.regexCache[key];
    }
    var tOptimistic = true; //shortcut expensive tests
    var tKey = ".^"; //match none, i.e. 1 letter before begin
    if (key == null) {
        tKey = ".^"; //match none
    }
    else {
        //       _    0 - 9....A..-..Z  a -  z..|   ?   *   .   ^   $
        if (!(tOptimistic ||
            /^[_0-9A-Za-z\x7C\x3F\x2A\x2E\x5E\x24]+$/.test(key))) {
            console.warn("Foreign key containing not allowed character: ", key, " -> match nothing;  Allowed ASCII _a..zA..Z0..9?*|");
            for (var i = 0; i < key.length; i++) {
                console.log(i +
                    " " +
                    key[i] +
                    (/[\x5F\x30-\x39\x41-\x5A\x61-\x7A\x7C\x3F\x2A\x2E\x5E\x24]/.test(key[i])
                        ? " ok "
                        : " NOT ALLOWED " + key.charCodeAt(i)));
            }
            //match nothing
        }
        else {
            if (key.indexOf("^") === 0) {
                tKey = key; //the key is probably already sanitized or a valid JS RegExp , at least IGX.Konva does its own handling ATM
                if (!tOptimistic) {
                    console.warn("createSaneRegExp called with seemingly already processed key: ", key);
                }
            }
            else {
                //     _0-9a-zA-Z  |   ?   *
                if (tOptimistic || /^[_0-9A-Za-z\x7C\x3F\x2A]+$/.test(key)) {
                    //OR handling
                    var tSplitOR = key.split("|");
                    var tSplitORsane = [];
                    try {
                        for (var tSplitOR_1 = __values(tSplitOR), tSplitOR_1_1 = tSplitOR_1.next(); !tSplitOR_1_1.done; tSplitOR_1_1 = tSplitOR_1.next()) {
                            var fragment = tSplitOR_1_1.value;
                            // tSplitOR.forEach(s => {
                            var s2 = "^"; //word start
                            s2 += fragment.replace("*", ".*"); // * -> any string of characters
                            s2 = s2.replace("?", "."); // ? -> any single character
                            s2 = s2 + "$"; //word end
                            tSplitORsane.push(s2);
                        }
                    }
                    catch (e_1_1) { e_1 = { error: e_1_1 }; }
                    finally {
                        try {
                            if (tSplitOR_1_1 && !tSplitOR_1_1.done && (_a = tSplitOR_1.return)) _a.call(tSplitOR_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                    }
                    tKey = tSplitORsane.join("|");
                }
                else {
                    console.warn("Foreign key ", key, " containing not allowed character");
                    console.warn("Allowed ASCII range from files:  _a..zA..Z0..9?*|");
                    //match none
                    for (var i = 0; i < key.length; i++) {
                        console.log(i +
                            " " +
                            key[i] +
                            (/[_0-9A-Za-z\x7C\x3F\x2A]/.test(key[i])
                                ? " ok "
                                : " NOT ALLOWED " + key.charCodeAt(i)));
                    }
                }
            }
        }
    }
    var tResult = null;
    try {
        tResult = new RegExp(tKey);
    }
    catch (e) {
        console.warn("Creating Regexp for key '", key, "' (via '", tKey, "') failed!");
        console.warn("Match nothing instead");
        tResult = new RegExp(".^"); //match none
    }
    //console.log("new RE");
    State.regexCache[key] = tResult;
    return tResult;
}
