"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMap = exports.extendIfUndefined = exports.addProperties = exports.clone = exports.extend = exports.matchProperties = void 0;
/**
 * Compares two PropertySets for equality.
 *
 * @internal
 */
function matchProperties(a, b) {
    if (!a && !b) {
        return true;
    }
    const keysA = a ? Object.keys(a) : [];
    const keysB = b ? Object.keys(b) : [];
    if (keysA.length !== keysB.length) {
        return false;
    }
    for (const key of keysA) {
        if (b?.[key] === undefined) {
            return false;
        }
        else if (typeof b[key] === "object") {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            if (!matchProperties(a?.[key], b[key])) {
                return false;
            }
        }
        else if (b[key] !== a?.[key]) {
            return false;
        }
    }
    return true;
}
exports.matchProperties = matchProperties;
/**
 * Adds properties from one PropertySet to another.
 *
 * @internal
 */
function extend(base, extension) {
    if (extension !== undefined) {
        for (const [key, v] of Object.entries(extension)) {
            if (v === undefined) {
                continue;
            }
            else if (v === null) {
                // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
                delete base[key];
            }
            else {
                base[key] = v;
            }
        }
    }
    return base;
}
exports.extend = extend;
/**
 * Clones properties in a given PropertySet into a new PropertySet.
 *
 * @internal
 */
function clone(extension) {
    if (extension === undefined) {
        return undefined;
    }
    const cloneMap = createMap();
    return extend(cloneMap, extension);
}
exports.clone = clone;
/**
 * Add properties in one PropertySet to another PropertySet. If the PropertySet we are adding
 * to does not exist, create one.
 *
 * @internal
 */
function addProperties(oldProps, newProps) {
    return extend(oldProps ?? createMap(), newProps);
}
exports.addProperties = addProperties;
/**
 * Replace values of undefined in one PropertySet with values for the same key from another PropertySet.
 *
 * @internal
 */
function extendIfUndefined(base, extension) {
    if (extension !== undefined) {
        for (const [key, value] of Object.entries(extension)) {
            // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- using ??= could change behavior if value is falsy
            if (base[key] === undefined) {
                base[key] = value;
            }
        }
    }
    return base;
}
exports.extendIfUndefined = extendIfUndefined;
/**
 * Create a MapLike with good performance.
 *
 * @internal
 */
function createMap() {
    return Object.create(null);
}
exports.createMap = createMap;
//# sourceMappingURL=properties.js.map