const objTargetString = Object.prototype.toString();
/**
 * This function checks if an object is an instance of one of the given types.
 * It works similarly to the `instanceof` operator, but works across frame boundaries.
 *
 * @param obj The object to check.
 * @param types The constructor functions or classes to check against.
 * @returns True if the object is an instance of one of the given types, false otherwise.
 */
const isInstance = (obj, ...types) => {
    if (types.length === 0)
        return false;
    if (!obj || (typeof obj !== 'object' && typeof obj !== 'function'))
        return false;
    if (types.some(type => obj instanceof type))
        return true;
    const targetStrings = new Set(types.flatMap(type => {
        const str = Object.prototype.toString.call(type.prototype);
        // If str is '[object Object]', but type.prototype is not Object.prototype bail to avoid false positives.
        if (str === objTargetString && type.prototype !== Object.prototype) {
            return [];
        }
        return str;
    }));
    if (targetStrings.size === 0)
        return false;
    let current = obj;
    while (current) {
        if (targetStrings.has(Object.prototype.toString.call(current))) {
            return true;
        }
        current = Object.getPrototypeOf(current);
    }
    return false;
};
export default isInstance;
//# sourceMappingURL=isInstance.js.map