var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { HTTP_METHOD, validateResponse } from '@darajs/ui-utils';
import { handleAuthErrors } from '../auth/auth';
import { request } from './http';
/**
 * Fetch the result of a task from the backend by it's id
 *
 * @param taskId the id of the task to fetch
 * @param token the session token to use
 */
export function fetchTaskResult(taskId, extras) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = yield request(`/api/core/tasks/${taskId}`, { method: HTTP_METHOD.GET }, extras);
        yield handleAuthErrors(res, { authenticationFailureRedirect: 'login' });
        if (res.status === 404) {
            return { status: 'not_found' };
        }
        yield validateResponse(res, `Failed to fetch the result of task with id: ${taskId}`);
        const resJson = yield res.json();
        if (typeof resJson === 'object' && 'error' in resJson) {
            throw new Error(resJson.error);
        }
        return { status: 'ok', result: resJson };
    });
}
/**
 * Cancel a task by it's id
 *
 * @param taskId the id of the task to fetch
 * @param token the session token to use
 */
export function cancelTask(taskId, extras) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = yield request(`/api/core/tasks/${taskId}`, { method: HTTP_METHOD.DELETE }, extras);
        yield handleAuthErrors(res, { authenticationFailureRedirect: 'login' });
        yield validateResponse(res, `Failed to cancel task with id: ${taskId}`);
        return true;
    });
}
//# sourceMappingURL=core.js.map