"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var debug_1 = __importDefault(require("debug"));
var log = (0, debug_1.default)("stub");
var logUse = (0, debug_1.default)("stub:use"); // log all use of the stub
var logFirst = (0, debug_1.default)("stub:first"); // log first use of the stub
function stubProxy(env, functionViaPointer) {
    return new Proxy(env, {
        get: function (target, key) {
            if (key in target) {
                return Reflect.get(target, key);
            }
            var f = functionViaPointer(key);
            if (f != null) {
                log("using function via pointer for ", key);
                return f;
            }
            log("creating stub", key);
            if (logUse.enabled || logFirst.enabled) {
                return function () {
                    var args = [];
                    for (var _i = 0; _i < arguments.length; _i++) {
                        args[_i] = arguments[_i];
                    }
                    logStubUse(key, args);
                    return 0;
                };
            }
            else {
                // faster to not trace or even check, obviously.
                return function () { return 0; };
            }
        },
    });
}
exports.default = stubProxy;
var stubUsed = new Set([]);
function logStubUse(functionName, args) {
    logUse("WARNING: using stub", functionName, args);
    if (logFirst.enabled) {
        if (stubUsed.has(functionName))
            return;
        stubUsed.add(functionName);
    }
    logFirst("WARNING: first use of stub", functionName, args);
}
//# sourceMappingURL=stub.js.map