/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useLocation } from './useLocation.js';
import { useNavigate } from './useNavigate.js';
import { useToast } from './useToast.js';
// import { useRuMnock } from './../mocks/index.js';
import { useIAMStore } from '../state/index.js';
import { requestDatalayerAPI } from '../api/DatalayerApi.js';
export function useDatalayer(props = {}) {
    const { loginRoute = '/login', notifyOnError = true } = props;
    const location = useLocation();
    /*
    // TODO Fix the conditional hook call.
    const coreStore = useCoreStore();
    // Don't remove === true for the test, otherwise it will always be true???
    if (coreStore.configuration.useMock === true) {
      return useDatalayerMock();
    }
    */
    const { enqueueToast } = useToast();
    const iamStore = useIAMStore();
    const navigate = useNavigate();
    const requestDatalayer = async (request) => {
        const { loginRoute: loginRoute_ = loginRoute, notifyOnError: notifyOnError_ = notifyOnError, ...apiRequest } = request;
        const token = apiRequest.token ?? iamStore.token;
        return requestDatalayerAPI({
            ...apiRequest,
            token,
        })
            .then(resp => {
            return resp;
        })
            .catch(error => {
            if (error.name === 'RunResponseError') {
                const responseError = error;
                if (responseError.response.status === 401) {
                    console.log('Datalayer sent a 401 return code.');
                    if (location.pathname !== loginRoute_) {
                        iamStore.logout();
                        navigate(loginRoute_);
                    }
                }
                else {
                    if (notifyOnError_) {
                        if (responseError.warnings) {
                            responseError.warnings.forEach(warning => enqueueToast(`${warning}`, { variant: 'warning' }));
                        }
                        if (responseError.errors) {
                            responseError.errors.forEach(error => enqueueToast(`${error}`, { variant: 'error' }));
                        }
                        if (responseError.message) {
                            enqueueToast(`${responseError.message}`, { variant: 'error' });
                        }
                        else {
                            enqueueToast(`API Error`, { variant: 'error' });
                        }
                        if (responseError.exceptionMessage) {
                            enqueueToast(`${responseError.exceptionMessage}`, {
                                variant: 'error',
                            });
                        }
                    }
                }
                const response = {
                    sucess: false,
                    message: responseError.message,
                    errors: responseError.errors,
                };
                return response;
            }
            // The error is not an RunResponseError...log it and throw it.
            console.log('Error', error);
            if (notifyOnError_) {
                enqueueToast(`${error.message}`, { variant: 'error' });
            }
            throw error;
        });
    };
    return { requestDatalayer };
}
export default useDatalayer;
