"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateIndexRange = exports.validateIndex = exports.validatePositiveIndex = exports.validateSafeInteger = void 0;
const internal_1 = require("@fluidframework/telemetry-utils/internal");
/**
 * Validates that the provided index is a safe integer.
 * @throws If the index is invalid.
 * @param index - The index to validate.
 * @param apiName - The name of the API performing the validation.
 * @throws If the index is invalid.
 */
function validateSafeInteger(index, apiName) {
    if (!Number.isSafeInteger(index)) {
        throw new internal_1.UsageError(`Expected a safe integer passed to ${apiName}, got ${index}.`);
    }
}
exports.validateSafeInteger = validateSafeInteger;
/**
 * Validates that the provided index is a 0 or greater safe integer.
 * @param index - The index to validate.
 * @param apiName - The name of the API performing the validation.
 * @throws If the index is invalid.
 */
function validatePositiveIndex(index, apiName) {
    validateSafeInteger(index, apiName);
    if (index < 0) {
        throw new internal_1.UsageError(`Expected non-negative index passed to ${apiName}, got ${index}.`);
    }
}
exports.validatePositiveIndex = validatePositiveIndex;
/**
 * Validates that the provided index is a non-negative safe integer within the bounds of the provided array (or, optionally, 1 past its end).
 * @throws If the index is invalid.
 * @param index - The index to validate.
 * @param array - The array to validate against.
 * @param apiName - The name of the API performing the validation.
 * @param allowOnePastEnd - Whether to allow the index to be one past the end of the array.
 */
function validateIndex(index, array, apiName, allowOnePastEnd = false) {
    validatePositiveIndex(index, apiName);
    if (allowOnePastEnd) {
        if (index > array.length) {
            throw new internal_1.UsageError(`Index value passed to ${apiName} is out of bounds. Expected at most ${array.length}, got ${index}.`);
        }
    }
    else {
        if (index >= array.length) {
            throw new internal_1.UsageError(`Index value passed to ${apiName} is out of bounds. Expected at most ${array.length - 1}, got ${index}.`);
        }
    }
}
exports.validateIndex = validateIndex;
/**
 * Validates that the provided range `[startIndex, endIndex)` is valid and within the bounds of the provided array.
 * @remarks
 * This is intended for ranges which are inclusive for the lower bound, and exclusive for the upper bound,
 * and permits 0 length ranges, even `[array.length, array.length)`.
 * @throws If the index is invalid.
 * @param startIndex - The index that starts the range (inclusive).
 * @param endIndex - The index that ends the range (exclusive).
 * @param array - The array to validate against.
 * @param apiName - The name of the API performing the validation.
 */
function validateIndexRange(startIndex, endIndex, array, apiName) {
    if (startIndex > endIndex) {
        throw new internal_1.UsageError(`Malformed range passed to ${apiName}. Start index ${startIndex} is greater than end index ${endIndex}.`);
    }
    validateIndex(startIndex, array, apiName, true);
    validateIndex(endIndex, array, apiName, true);
}
exports.validateIndexRange = validateIndexRange;
//# sourceMappingURL=arrayUtilities.js.map