"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIsType = void 0;
const t = __importStar(require("../types"));
//  key = type, value = all types implemented by this type
class TypeData {
    constructor(baseType, ...types) {
        this.baseType = baseType.path.toString();
        this.types = new Set(types.map(type => type.path.toString()));
        this.types.add(this.baseType);
    }
}
//  type, [baseType], ...otherBaseTypesTheMainTypeImplements
let baseTypes = [
    [t.Boolean],
    [t.String],
    [t.Number],
    [t.Integer, t.Number],
    [t.Symbol],
    [t.Null, t.Null, t.Undefined],
    [t.Undefined, t.Null, t.Null],
    [t.Object],
    [t.Array, t.Array, t.Object],
    [t.RegExp, t.RegExp, t.Object],
    [t.Map, t.Map, t.Object],
    [t.Set, t.Set, t.Object],
    [t.WeakMap, t.Map, t.Object],
    [t.Function, t.Function, t.Object],
    [t.Class, t.Function, t.Type],
    [t.Type, t.Object],
];
function getTypeMap(types) {
    let typeMap = new Map();
    for (let typeArray of types) {
        let [type, baseType = type, ...types] = typeArray;
        typeMap.set(type.path.toString(), new TypeData(baseType, ...types));
    }
    // now make sure we recursively add subtypes
    function addRecursive(type, types, added = new Set()) {
        var _a;
        if (types) {
            let data = typeMap.get(type);
            for (let addType of types) {
                if (!added.has(addType)) {
                    added.add(addType);
                    data.types.add(addType);
                    addRecursive(type, (_a = typeMap.get(addType)) === null || _a === void 0 ? void 0 : _a.types, added);
                }
            }
        }
    }
    for (let type of typeMap.keys()) {
        addRecursive(type, typeMap.get(type).types);
    }
    return typeMap;
}
function createIsType(types) {
    let typeMap = getTypeMap([...baseTypes, ...types]);
    return (checkIfType, isInstanceOfOtherType) => {
        var _a, _b;
        let checkPath = (_a = checkIfType.path) === null || _a === void 0 ? void 0 : _a.toString();
        let otherPath = (_b = isInstanceOfOtherType.path) === null || _b === void 0 ? void 0 : _b.toString();
        if (checkPath == null || otherPath == null) {
            console.log("TODO: Figure out why isType#createIsType path's aren't present, possibly because of cyclic references");
            return null;
        }
        let checkData = typeMap.get(checkPath);
        let otherData = typeMap.get(otherPath);
        if (checkData == null || otherData == null) {
            return null;
        }
        if (checkData.types.has(otherPath)) {
            //  it definitely does implement it
            return true;
        }
        if (checkData.baseType !== otherData.baseType) {
            // if two types have a different base type then they absolutely cannot implement each other
            //  it definitely does NOT implement it
            return false;
        }
        // it MIGHT implement it
        return null;
    };
}
exports.createIsType = createIsType;
