import { usePermissionAutoFetch as usePlatformPermissionAutoFetch } from '@adminide-stack/platform-client';

interface userPermissionAutoFetchProps {
    configKey?: string;
}
export const usePermissionAutoFetch = (options?: userPermissionAutoFetchProps) => {
    const { permissions, loading } = usePlatformPermissionAutoFetch(options);

    return {
        permissions,
        data: permissions === undefined ? undefined : { resolveConfiguration: permissions },
        loading,
    };
};

// import React from 'react';
// import { ResourceAuthority, ConfigFragmentName } from '@adminide-stack/core';
// import { useGetTeamContextQuery } from '@adminide-stack/platform-client';
// import { generateOrgUri } from '@adminide-stack/core/lib/utils/generate-uri.js';
// import { useSelector } from 'react-redux';
// import { navigationRef } from '@common-stack/client-react';
// import { useSetting } from './useSetting';

// interface userPermissionAutoFetchProps {
//     configKey?: string;
// }
// export const usePermissionAutoFetch = (options?: userPermissionAutoFetchProps) => {
//     const [orgName, setOrgName] = React.useState<any>(null);
//     const [orgUri, setOrgUri] = React.useState<any>(null);
//     const platformState = useSelector((state) => (state as any).platform);
//     const currentRoute: any = navigationRef.isReady() ? navigationRef?.getCurrentRoute() : null;
//     const { data, loading } = useGetTeamContextQuery();
//     const teamData = React.useMemo(() => data?.getTeamContext, [data]);
//     React.useEffect(() => {
//         if (currentRoute?.params?.orgName || platformState?.orgName) {
//             const org = currentRoute?.params?.orgName || platformState?.orgName;
//             if (org !== orgName) {
//                 setOrgName(org);
//             }
//         }
//         if (orgName) {
//             const orgUri = generateOrgUri(orgName, ConfigFragmentName.Settings);
//             setOrgUri(orgUri);
//         }
//     }, [currentRoute, platformState, orgName]);

//     const { loading: settingLoading, ...remaining } = useSetting({
//         configKey: options?.configKey || '',
//         overrides: {
//             resource: teamData?.teamUri ?? orgUri,
//         },
//         options: {
//             forceExist: false,
//             authority: ResourceAuthority.Defaultpermissions,
//             fragment: ConfigFragmentName.Roles,
//         },
//     });
//     return { ...remaining, loading: loading || settingLoading };
// };
