import { __require as requireType } from "../../es-errors/type.js";
import { __require as requireMaxSafeInteger } from "../../math-intrinsics/constants/maxSafeInteger.js";
import { __require as requireCall } from "./Call.js";
import { __require as requireCreateDataPropertyOrThrow } from "./CreateDataPropertyOrThrow.js";
import { __require as requireGet } from "./Get.js";
import { __require as requireHasProperty } from "./HasProperty.js";
import { __require as requireIsArray } from "./IsArray.js";
import { __require as requireLengthOfArrayLike } from "./LengthOfArrayLike.js";
import { __require as requireToString } from "./ToString.js";
var FlattenIntoArray;
var hasRequiredFlattenIntoArray;
function requireFlattenIntoArray() {
  if (hasRequiredFlattenIntoArray) return FlattenIntoArray;
  hasRequiredFlattenIntoArray = 1;
  var $TypeError = requireType();
  var MAX_SAFE_INTEGER = requireMaxSafeInteger();
  var Call = requireCall();
  var CreateDataPropertyOrThrow = requireCreateDataPropertyOrThrow();
  var Get = requireGet();
  var HasProperty = requireHasProperty();
  var IsArray = requireIsArray();
  var LengthOfArrayLike = requireLengthOfArrayLike();
  var ToString = requireToString();
  FlattenIntoArray = function FlattenIntoArray2(target, source, sourceLen, start, depth) {
    var mapperFunction;
    if (arguments.length > 5) {
      mapperFunction = arguments[5];
    }
    var targetIndex = start;
    var sourceIndex = 0;
    while (sourceIndex < sourceLen) {
      var P = ToString(sourceIndex);
      var exists = HasProperty(source, P);
      if (exists === true) {
        var element = Get(source, P);
        if (typeof mapperFunction !== "undefined") {
          if (arguments.length <= 6) {
            throw new $TypeError("Assertion failed: thisArg is required when mapperFunction is provided");
          }
          element = Call(mapperFunction, arguments[6], [element, sourceIndex, source]);
        }
        var shouldFlatten = false;
        if (depth > 0) {
          shouldFlatten = IsArray(element);
        }
        if (shouldFlatten) {
          var elementLen = LengthOfArrayLike(element);
          targetIndex = FlattenIntoArray2(target, element, elementLen, targetIndex, depth - 1);
        } else {
          if (targetIndex >= MAX_SAFE_INTEGER) {
            throw new $TypeError("index too large");
          }
          CreateDataPropertyOrThrow(target, ToString(targetIndex), element);
          targetIndex += 1;
        }
      }
      sourceIndex += 1;
    }
    return targetIndex;
  };
  return FlattenIntoArray;
}
export {
  requireFlattenIntoArray as __require
};
