import { client } from '@ottocode/api';
import { API_BASE_URL } from '../config';
export function extractErrorMessage(error) {
    if (!error)
        return 'Unknown error';
    if (typeof error === 'string')
        return error;
    if (error && typeof error === 'object') {
        const errObj = error;
        if (errObj.error && typeof errObj.error === 'object') {
            const innerError = errObj.error;
            if (typeof innerError.message === 'string')
                return innerError.message;
        }
        if (typeof errObj.error === 'string')
            return errObj.error;
        if (typeof errObj.message === 'string')
            return errObj.message;
        try {
            return JSON.stringify(error);
        }
        catch {
            return 'Error occurred (unable to parse)';
        }
    }
    return 'Unknown error';
}
export function configureApiClient() {
    const win = window;
    const baseURL = win.OTTO_SERVER_URL || API_BASE_URL;
    client.setConfig({ baseURL });
}
configureApiClient();
export function getBaseUrl() {
    const win = window;
    if (win.OTTO_SERVER_URL)
        return win.OTTO_SERVER_URL;
    return API_BASE_URL;
}
export function convertSession(apiSession) {
    return {
        ...apiSession,
        title: apiSession.title ?? null,
        createdAt: typeof apiSession.createdAt === 'string'
            ? new Date(apiSession.createdAt).getTime()
            : apiSession.createdAt,
        lastActiveAt: typeof apiSession.lastActiveAt === 'string'
            ? new Date(apiSession.lastActiveAt).getTime()
            : apiSession.lastActiveAt,
    };
}
export function convertMessage(apiMessage) {
    return {
        ...apiMessage,
        createdAt: typeof apiMessage.createdAt === 'string'
            ? new Date(apiMessage.createdAt).getTime()
            : apiMessage.createdAt,
        completedAt: apiMessage.completedAt
            ? typeof apiMessage.completedAt === 'string'
                ? new Date(apiMessage.completedAt).getTime()
                : apiMessage.completedAt
            : null,
    };
}
//# sourceMappingURL=utils.js.map