{"version":3,"file":"useOutlook.mjs","names":["apiMutate","useEffect","ConfigService","useOutlook","callbackUri","onLogin","invalidateUserSession","error","console","localStorage","removeItem","getAccessToken","accessToken","getItem","Error","expiresAt","isBeforeCurrentTime","refreshToken","integrationV2ApiUrl","undefined","response","method","data","option","value","redirectUri","config","UNITY_URL","expiresIn","setItem","addSecondsToCurrentTimeUnix","login","baseUrl","clientId","process","env","REACT_APP_MSAL_API_CLIENT_ID","MSAL_API_ACCESS_SCOPE","split","responseType","responseMode","scope","join","authUrl","openLoginPopup","url","width","height","left","screen","top","features","popup","window","open","seconds","now","Date","setSeconds","getSeconds","getTime","unixTime","isUserSignedIn","handleStorageChange","event","key","addEventListener","removeEventListener"],"sources":["../../src/hooks/useOutlook.js"],"sourcesContent":["import { apiMutate } from '../utilities/useAxiosMutate';\nimport { useEffect } from 'react';\nimport { ConfigService } from '../configService';\n\nexport const useOutlook = (callbackUri, onLogin) => {\n\n    const invalidateUserSession = async () => {\n        try {\n            // if (ConfigService.integrationV2ApiUrl === undefined) {\n            //     throw new Error('REACT_APP_INTEGRATION_V2_API_BASE is undefined')\n            // }\n            // const accessToken = localStorage.getItem('outlookAccessToken');\n            // const response = await apiMutate(\n            //     ConfigService.integrationV2ApiUrl,\n            //     `OutlookIntegrationAccessToken/invalidateUserSession`,\n            //     {\n            //         method: \"post\",\n            //         headers : {\n            //             OutlookAccessToken: accessToken\n            //         }\n            //     }\n            // )\n\n        } catch (error) {\n            console.error('Error refreshing access token:', error);\n            return null;\n        // }\n            } finally {\n            try {\n                localStorage.removeItem('outlookAccessToken');\n              } catch (error) {\n                console.error('Error removing accessToken:', error);\n              }\n\n              try {\n                localStorage.removeItem('outlookRefreshToken');\n              } catch (error) {\n                console.error('Error removing refreshToken:', error);\n              }\n\n              try {\n                localStorage.removeItem('outlookExpiresAt');\n              } catch (error) {\n                console.error('Error removing expiresAt:', error);\n              }\n\n        }\n    }\n\n    const getAccessToken = async () => {\n        let accessToken = localStorage.getItem('outlookAccessToken');\n\n        if (!accessToken) {\n            throw(new Error(\"No access token found\"));\n        }\n\n        const expiresAt = localStorage.getItem('outlookExpiresAt');\n        if (expiresAt && !isBeforeCurrentTime(expiresAt)) {\n            return accessToken;\n        }\n\n        const refreshToken = localStorage.getItem('outlookRefreshToken');\n        if (!refreshToken) {\n            throw(new Error(\"No refresh token found\"));\n        }\n\n        try {\n            if (ConfigService.integrationV2ApiUrl === undefined) {\n                throw new Error('REACT_APP_INTEGRATION_V2_API_BASE is undefined')\n            }\n            const response = await apiMutate(\n                ConfigService.integrationV2ApiUrl,\n                `OutlookIntegrationAccessToken/getAccessToken`,\n                {\n                    method: \"post\",\n                    data: {\n                        option: \"refresh_token\",\n                        value: refreshToken,\n                        redirectUri: ConfigService.config.UNITY_URL + \"/aad_redirect\"\n                    }\n                }\n            )\n            const data = response.data;\n            if (data.accessToken != null && data.refreshToken != null && data.expiresIn != null) {\n              localStorage.setItem('outlookAccessToken', data.accessToken);\n              localStorage.setItem('outlookRefreshToken', data.refreshToken);\n              localStorage.setItem('outlookExpiresAt', addSecondsToCurrentTimeUnix(data.expiresIn));\n              return data.accessToken;\n            } else {\n                throw(new Error(\"Error: unable to get access token\"));\n            }\n\n        } catch (error) {\n            console.error('Error refreshing access token:', error);\n            return null;\n        }\n    };\n\n    const login = () => {\n\n        const baseUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';\n        let clientId;\n        // if the process is defined (using the vite-plugin-environment) use the env variable\n        if(process !== undefined)\n            // eslint-disable-next-line custom-rules/no-process-env\n            clientId = process.env.REACT_APP_MSAL_API_CLIENT_ID;\n        // otherwise use the config service and strip the scope part so its just the guid\n        else\n            clientId = ConfigService.config.MSAL_API_ACCESS_SCOPE?.split('/')[0];\n\n        const responseType = 'code';\n        const redirectUri = `${ConfigService.config.UNITY_URL}/aad_redirect`;\n        const responseMode = 'query';\n        const scope = [\n            'https://graph.microsoft.com/Calendars.ReadWrite',\n            'https://graph.microsoft.com/Calendars.ReadWrite.Shared',\n            'https://graph.microsoft.com/OnlineMeetingTranscript.Read.All',\n            'https://graph.microsoft.com/OnlineMeetings.Read',\n        ].join('%20'); // Join scopes with URL-encoded space\n\n        const authUrl = `${baseUrl}?client_id=${clientId}&response_type=${responseType}&redirect_uri=${redirectUri}&response_mode=${responseMode}&scope=${scope}&state=${callbackUri}`;\n        openLoginPopup(authUrl)\n        // window.location.href = authUrl;\n    }\n\n\n    function openLoginPopup(url) {\n        var width = 600;\n        var height = 600;\n        var left = (screen.width - width) / 2;\n        var top = (screen.height - height) / 2;\n        var features = `width=${width},height=${height},top=${top},left=${left},status=no,toolbar=no,menubar=no,location=no`;\n\n        var popup = window.open(url, 'LoginPopup', features);\n        return popup;\n    }\n    function addSecondsToCurrentTimeUnix(seconds) {\n        var now = new Date();\n        now.setSeconds(now.getSeconds() + seconds);\n        return now.getTime();\n    }\n\n    function isBeforeCurrentTime(unixTime) {\n        var now = new Date().getTime();\n        return unixTime < now;\n    }\n\n    const isUserSignedIn = () => {\n        const accessToken = localStorage.getItem('outlookAccessToken');\n        const refreshToken = localStorage.getItem('outlookRefreshToken');\n        const expiresAt = localStorage.getItem('outlookExpiresAt');\n        if (accessToken && refreshToken && expiresAt && !isBeforeCurrentTime(expiresAt)) {\n            return true;\n        } else {\n            return false;\n        }\n    }\n\n    useEffect(() => {\n        const handleStorageChange = (event) => {\n          if (event.key === 'outlookAccessToken') {\n            onLogin();\n          }\n        };\n\n        window.addEventListener('storage', handleStorageChange);\n\n        // Clean up the event listener\n        return () => {\n          window.removeEventListener('storage', handleStorageChange);\n        };\n      }, []);\n    return {getAccessToken, invalidateUserSession, login, isUserSignedIn};\n}\n"],"mappings":";;;;AAIA,MAAaG,cAAcC,aAAaC,YAAY;CAEhD,MAAMC,wBAAwB,YAAY;AAsBlC,MAAI;AACAG,gBAAaC,WAAW,qBAAqB;WACtCH,OAAO;AACdC,WAAQD,MAAM,+BAA+BA,MAAM;;AAGrD,MAAI;AACFE,gBAAaC,WAAW,sBAAsB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