export const notificationCache = new Map();
export const initializeSession = async ({ baseUrl, body, sessionId }) => {
    const url = new URL(`${baseUrl}/widgets/session/initialize`);
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', sessionId);
    const options = {
        method: 'POST',
        body: JSON.stringify(body),
        headers
    };
    try {
        const res = await fetch(url.href, options);
        if (!res.ok) {
            return {
                token: null,
                unseenCounter: 0
            };
        }
        const { data } = await res.json();
        const newToken = data === null || data === void 0 ? void 0 : data.token;
        headers.append('Widget-Authorization', `Bearer ${newToken}`);
        const unseenCounter = await getUnseenCount({
            baseUrl,
            token: newToken
        });
        return {
            token: newToken,
            unseenCounter
        };
    }
    catch (error) {
        console.log(error);
        return {
            token: null,
            unseenCounter: 0
        };
    }
};
export const getUnseenCount = async ({ baseUrl, token }) => {
    var _a;
    const url = new URL(`${baseUrl}/widgets/notifications/unseen?limit=100`);
    const headers = new Headers();
    headers.append('Widget-Authorization', `Bearer ${token}`);
    const options = {
        method: 'GET',
        headers
    };
    try {
        const res = await fetch(url.href, options);
        const data = await res.json();
        const unseenCount = ((_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.count) || 0;
        return unseenCount;
    }
    catch (error) {
        console.log(error);
        return 0;
    }
};
export const deleteMessage = async ({ baseUrl, token, messageId }) => {
    const url = new URL(`${baseUrl}/widgets/messages/${messageId}`);
    const headers = new Headers();
    headers.append('Widget-Authorization', `Bearer ${token}`);
    const options = {
        method: 'DELETE',
        headers
    };
    try {
        await fetch(url.href, options);
        return false;
    }
    catch (error) {
        console.log(error);
        return true;
    }
};
export const markAsRead = async ({ baseUrl, body, token, unread }) => {
    const url = new URL(`${baseUrl}/widgets/messages/markAs`);
    const headers = new Headers();
    headers.append('Widget-Authorization', `Bearer ${token}`);
    headers.append('Content-Type', 'application/json');
    const options = {
        method: 'POST',
        headers,
        body: JSON.stringify(body)
    };
    try {
        const res = await fetch(url.href, options);
        const data = await res.json();
        return {
            messageSeen: data.data[0].seen,
            messageRead: data.data[0].read,
            showSettingsModal: false
        };
    }
    catch (err) {
        console.error('err', err);
        return {
            messageSeen: unread,
            messageRead: unread,
            showSettingsModal: true
        };
    }
};
export const getNotifications = async ({ baseUrl, page, token }) => {
    const url = new URL(`${baseUrl}/widgets/notifications/feed`);
    const headers = new Headers();
    url.searchParams.append('page', page.toString());
    headers.append('Widget-Authorization', `Bearer ${token}`);
    const options = {
        method: 'GET',
        headers
    };
    try {
        const res = await fetch(url.href, options);
        const data = await res.json();
        return {
            numberOfNotifications: data.totalCount,
            notifications: data.data,
            hasMore: data.hasMore
        };
    }
    catch (error) {
        console.log(error);
        return {
            numberOfNotifications: 0,
            notifications: [],
            hasMore: undefined
        };
    }
};
export const markAllAsRead = async ({ baseUrl, token }) => {
    const url = new URL(`${baseUrl}/widgets/messages/read`);
    const headers = new Headers();
    headers.append('Widget-Authorization', `Bearer ${token}`);
    headers.append('Content-Type', 'application/json');
    const options = {
        method: 'POST',
        body: JSON.stringify({}),
        headers
    };
    try {
        await fetch(url.href, options);
        return true;
    }
    catch (error) {
        console.log(error);
        return false;
    }
};
export const deleteAll = async ({ baseUrl, token }) => {
    const url = new URL(`${baseUrl}/widgets/messages`);
    const headers = new Headers();
    headers.append('Widget-Authorization', `Bearer ${token}`);
    const options = {
        method: 'DELETE',
        headers
    };
    try {
        await fetch(url.href, options);
        return true;
    }
    catch (error) {
        console.log(error);
        return false;
    }
};
