import React, { useContext, useMemo } from 'react';
import { useConfigPermissions } from '../../hooks/useConfigPermissions';
import { isTempUri } from '@reltio/mdm-sdk';
var defaultValue = {
    canCreate: true,
    canUpdate: true,
    canDelete: true
};
export var ConfigPermissionsContext = React.createContext(defaultValue);
ConfigPermissionsContext.displayName = 'ConfigPermissionsContext';
export var ConfigPermissionsContextProvider = function (_a) {
    var children = _a.children, view = _a.view;
    var _b = useConfigPermissions(), canCreate = _b.canCreate, canDelete = _b.canDelete, canUpdate = _b.canUpdate;
    var value = useMemo(function () {
        return view
            ? {
                canCreate: canCreate(view),
                canUpdate: canUpdate(view),
                canDelete: canDelete(view)
            }
            : defaultValue;
    }, [view, canCreate, canUpdate, canDelete]);
    return React.createElement(ConfigPermissionsContext.Provider, { value: value }, children);
};
export var useAttributeValueConfigPermissions = function (attributeValueUri) {
    var _a = useContext(ConfigPermissionsContext), canCreate = _a.canCreate, canUpdate = _a.canUpdate, canDelete = _a.canDelete;
    var isNew = isTempUri(attributeValueUri);
    return useMemo(function () { return ({
        canCreate: canCreate,
        canUpdate: isNew || canUpdate,
        canDelete: isNew || canDelete
    }); }, [isNew, canCreate, canUpdate, canDelete]);
};
