"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyIsValidIndex = exports.getOwnArrayKeys = exports.adaptWithProxy = exports.isPrimitive = void 0;
const core_1 = require("../../core");
const defaultFieldKinds_1 = require("../defaultFieldKinds");
/**
 * @returns true iff `schema` trees should default to being viewed as just their value when possible.
 *
 * @remarks
 * This may return true for some types which EditableTree does not unwrap to just their value,
 * since EditableTree avoids ever unwrapping primitives that are objects
 * so users checking for primitives by type won't be broken.
 * Checking for this object case is done elsewhere.
 * @alpha
 */
function isPrimitive(schema) {
    // TODO: use a separate `ITreeSchema` type, with metadata that determines if the type is primitive.
    // Since the above is not done yet, use use a heuristic:
    return (schema.value !== core_1.ValueSchema.Nothing &&
        schema.localFields.size === 0 &&
        schema.globalFields.size === 0 &&
        schema.extraGlobalFields === false &&
        schema.extraLocalFields.kind.identifier === defaultFieldKinds_1.forbidden.identifier);
}
exports.isPrimitive = isPrimitive;
function adaptWithProxy(target, proxyHandler) {
    // Proxy constructor assumes handler emulates target's interface.
    // Ours does not, so this cast is required.
    return new Proxy(target, proxyHandler);
}
exports.adaptWithProxy = adaptWithProxy;
function getOwnArrayKeys(length) {
    return Object.getOwnPropertyNames(Array.from(Array(length)));
}
exports.getOwnArrayKeys = getOwnArrayKeys;
function keyIsValidIndex(key, length) {
    const index = Number(key);
    if (typeof key === "string" && String(index) !== key)
        return false;
    return Number.isInteger(index) && 0 <= index && index < length;
}
exports.keyIsValidIndex = keyIsValidIndex;
//# sourceMappingURL=utilities.js.map