var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
import { areOneHierarchyUris } from '@reltio/mdm-sdk';
export var SyncedExpandedAttributesContext = createContext(null);
export var SyncedExpandedAttributesProvider = function (_a) {
    var children = _a.children;
    var _b = useState([]), expandedAttributes = _b[0], setExpandedAttributes = _b[1];
    var isExpanded = useCallback(function (attributeUri) { return expandedAttributes.includes(attributeUri); }, [expandedAttributes]);
    var toggle = useCallback(function (attributeUri) {
        setExpandedAttributes(function (prev) {
            if (prev.includes(attributeUri)) {
                return prev.filter(function (uri) { return !areOneHierarchyUris(uri, attributeUri); });
            }
            return __spreadArray(__spreadArray([], prev, true), [attributeUri], false);
        });
    }, []);
    var value = useMemo(function () { return ({ isExpanded: isExpanded, toggle: toggle }); }, [isExpanded, toggle]);
    return (React.createElement(SyncedExpandedAttributesContext.Provider, { value: value }, children));
};
export var useSyncedExpandedAttributes = function () { return useContext(SyncedExpandedAttributesContext); };
