import { apiMutate } from '../../utilities/useAxiosMutate';
import { ConfigService } from '../../configService';
const CONTROLLER = 'OutlookIntegrationToken';
const OUTLOOK_CALLBACK_PATH = '/auth/outlook/callback';
export async function getStatus() {
    const { data } = (await apiMutate(ensureBaseUrl(), `${CONTROLLER}/status`, { method: 'get', headers: productHeaders() }, undefined));
    return data !== null && data !== void 0 ? data : { connected: false };
}
export async function getAuthorizeUrl(request) {
    const { data } = (await apiMutate(ensureBaseUrl(), `${CONTROLLER}/authorize-url`, {
        method: 'post',
        headers: productHeaders(),
        data: request,
    }, undefined));
    return data;
}
export async function exchangeAuthorizationCode(request) {
    const { data } = (await apiMutate(ensureBaseUrl(), `${CONTROLLER}/oauth/exchange`, {
        method: 'post',
        headers: productHeaders(),
        data: request,
    }, undefined));
    return data;
}
export async function disconnect() {
    await apiMutate(ensureBaseUrl(), `${CONTROLLER}/disconnect`, {
        method: 'post',
        headers: productHeaders(),
    }, undefined);
}
export async function sendMail(payload) {
    await apiMutate(ensureBaseUrl(), `${CONTROLLER}/send-mail`, {
        method: 'post',
        headers: productHeaders(),
        data: payload,
    }, undefined);
}
export async function checkAndAuthenticate() {
    await apiMutate(ensureBaseUrl(), `${CONTROLLER}/authenticate`, {
        method: 'post',
        headers: productHeaders(),
    }, undefined);
}
export function getOutlookRedirectUri() {
    var _a;
    var _b;
    if (typeof window !== 'undefined' && window.location) {
        return `${window.location.origin}${OUTLOOK_CALLBACK_PATH}`;
    }
    const base = String((_b = (_a = ConfigService === null || ConfigService === void 0 ? void 0 : ConfigService.config) === null || _a === void 0 ? void 0 : _a.UNITY_URL) !== null && _b !== void 0 ? _b : '').replace(/\/$/, '');
    return `${base}${OUTLOOK_CALLBACK_PATH}`;
}
function ensureBaseUrl() {
    return ConfigService.integrationV2ApiUrl;
}
function productHeaders() {
    var _a;
    const rawId = (_a = ConfigService === null || ConfigService === void 0 ? void 0 : ConfigService.config) === null || _a === void 0 ? void 0 : _a.UNITY_PRODUCT_ID;
    const normalized = String(rawId !== null && rawId !== void 0 ? rawId : '').trim();
    if (!normalized) {
        throw new Error('UNITY_PRODUCT_ID is not configured.');
    }
    const numericId = Number(normalized);
    if (!Number.isFinite(numericId) || numericId <= 0) {
        throw new Error('UNITY_PRODUCT_ID must be a positive number.');
    }
    return { productId: String(numericId) };
}
//# sourceMappingURL=outlookIntegrationClient.js.map