/**
 * https://github.com/Shopify/polaris/blob/main/polaris-react/src/utilities/get.ts#L3
 */
const OBJECT_NOTATION_MATCHER = /(\w+)/g;
export function get(keypath, objs) {
    const keys = Array.isArray(keypath) ? keypath : getKeypath(keypath);
    for (const obj of objs) {
        if (!obj) {
            continue;
        }
        let acc = obj;
        for (let i = 0; i < keys.length; i++) {
            const val = acc[keys[i]];
            if (val === undefined) {
                continue;
            }
            acc = val;
        }
        if (typeof acc === "string") {
            return acc;
        }
    }
    throw new Error(`Error translating key. Keypath '${keypath}' does not resolve to a string.`);
}
const keypathCache = new Map();
function getKeypath(str) {
    const cached = keypathCache.get(str);
    if (cached) {
        return cached;
    }
    const path = [];
    let result = OBJECT_NOTATION_MATCHER.exec(str);
    while (result) {
        const [, first, second] = result;
        path.push(first || second);
        result = OBJECT_NOTATION_MATCHER.exec(str);
    }
    keypathCache.set(str, path);
    return path;
}
//# sourceMappingURL=get.js.map