"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const _commonjsHelpers = require("../../_virtual/_commonjsHelpers.cjs");
const index = require("../for-each/index.cjs");
const index$1 = require("../available-typed-arrays/index.cjs");
const index$3 = require("../call-bind/index.cjs");
const callBound$1 = require("../call-bind/callBound.cjs");
const index$2 = require("../gopd/index.cjs");
const shams = require("../has-tostringtag/shams.cjs");
var forEach = index.forEach_1;
var availableTypedArrays = index$1.availableTypedArrays;
var callBind = index$3.callBindExports;
var callBound = callBound$1.callBound;
var gOPD = index$2.gopd;
var $toString = callBound("Object.prototype.toString");
var hasToStringTag = shams.shams();
var g = typeof globalThis === "undefined" ? _commonjsHelpers.commonjsGlobal : globalThis;
var typedArrays = availableTypedArrays();
var $slice = callBound("String.prototype.slice");
var getPrototypeOf = Object.getPrototypeOf;
var $indexOf = callBound("Array.prototype.indexOf", true) || function indexOf(array, value) {
  for (var i = 0; i < array.length; i += 1) {
    if (array[i] === value) {
      return i;
    }
  }
  return -1;
};
var cache = { __proto__: null };
if (hasToStringTag && gOPD && getPrototypeOf) {
  forEach(typedArrays, function(typedArray) {
    var arr = new g[typedArray]();
    if (Symbol.toStringTag in arr) {
      var proto = getPrototypeOf(arr);
      var descriptor = gOPD(proto, Symbol.toStringTag);
      if (!descriptor) {
        var superProto = getPrototypeOf(proto);
        descriptor = gOPD(superProto, Symbol.toStringTag);
      }
      cache["$" + typedArray] = callBind(descriptor.get);
    }
  });
} else {
  forEach(typedArrays, function(typedArray) {
    var arr = new g[typedArray]();
    var fn = arr.slice || arr.set;
    if (fn) {
      cache["$" + typedArray] = callBind(fn);
    }
  });
}
var tryTypedArrays = function tryAllTypedArrays(value) {
  var found = false;
  forEach(
    // eslint-disable-next-line no-extra-parens
    /** @type {Record<`\$${TypedArrayName}`, Getter>} */
    /** @type {any} */
    cache,
    /** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void} */
    function(getter, typedArray) {
      if (!found) {
        try {
          if ("$" + getter(value) === typedArray) {
            found = $slice(typedArray, 1);
          }
        } catch (e) {
        }
      }
    }
  );
  return found;
};
var trySlices = function tryAllSlices(value) {
  var found = false;
  forEach(
    // eslint-disable-next-line no-extra-parens
    /** @type {Record<`\$${TypedArrayName}`, Getter>} */
    /** @type {any} */
    cache,
    /** @type {(getter: typeof cache, name: `\$${import('.').TypedArrayName}`) => void} */
    function(getter, name) {
      if (!found) {
        try {
          getter(value);
          found = $slice(name, 1);
        } catch (e) {
        }
      }
    }
  );
  return found;
};
var whichTypedArray = function whichTypedArray2(value) {
  if (!value || typeof value !== "object") {
    return false;
  }
  if (!hasToStringTag) {
    var tag = $slice($toString(value), 8, -1);
    if ($indexOf(typedArrays, tag) > -1) {
      return tag;
    }
    if (tag !== "Object") {
      return false;
    }
    return trySlices(value);
  }
  if (!gOPD) {
    return null;
  }
  return tryTypedArrays(value);
};
exports.whichTypedArray = whichTypedArray;
//# sourceMappingURL=index.cjs.map
