import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, } from './SdkBase.js';
import { RouteBuilder } from './RouteBuilder.js';
import { Kind as HttpHistoryKind } from '../../models/HttpHistory.js';
import { SdkError } from './Errors.js';
export class HistorySdk extends SdkBase {
    /**
     * Creates a history object.
     * A history object can be created per app (type = app) or a store's organization/[project/[request]].
     *
     * The user can always read their own history. If the history is created for a organization/project then
     * history records are shared as any other object in the organization.
     *
     * Note, history objects cannot be updated. They can only be created or deleted.
     *
     * @param history The history to create
     * @param request Optional request options.
     * @returns The key of the created history.
     */
    async add(history, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.history());
        const body = JSON.stringify(history);
        const result = await this.sdk.http.post(url.toString(), { token, body });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to create a history. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
        if (!result.body) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
        }
        if (!data.key) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
        }
        return data;
    }
    /**
     * Creates a multiple history objects in a batch operation.
     *
     * @param info The bulk create info object.
     * @param request Optional request options.
     */
    async createBulk(info, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.historyBatchCreate());
        const body = JSON.stringify(info);
        const result = await this.sdk.http.post(url.toString(), { token, body });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to create a bulk history. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
        if (!result.body) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Error(`${E_PREFIX}${E_INVALID_JSON}`);
        }
        if (!data.items) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`);
        }
        return data;
    }
    /**
     * Lists the history.
     *
     * @param options Optional query options.
     * @param request Optional request options.
     */
    async list(options, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.history());
        this.appendHistoryListParameters(url, options);
        const result = await this.sdk.http.get(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to list history. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
        if (!result.body) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
        }
        if (!Array.isArray(data.items)) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
        }
        return data;
    }
    appendHistoryListParameters(url, options) {
        this.sdk.appendListOptions(url, options);
        const { searchParams } = url;
        searchParams.set('organization', options.organization);
        if (options.type) {
            searchParams.set('type', options.type);
        }
        const projectOptions = options;
        if (projectOptions.id) {
            searchParams.set('id', projectOptions.id);
        }
        if (projectOptions.user) {
            searchParams.set('user', 'true');
        }
        if (projectOptions.project) {
            searchParams.set('project', projectOptions.project);
        }
    }
    /**
     * Deletes a history object form the store.
     *
     * @param key The key returned by the store when created the history. Also available via the
     *   `key` property on the history object.
     * @param request Optional request options.
     */
    async delete(key, request = {}) {
        const { token } = request;
        const path = RouteBuilder.historyItem(key);
        const url = this.sdk.getUrl(path);
        const result = await this.sdk.http.delete(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to delete history. ';
        if (result.status !== 204) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
    }
    /**
     * Deletes history in a bulk operation.
     * @param keys The list of keys of history objects to delete.
     * @param request Optional request options.
     * @returns The list of delete records.
     */
    async deleteBulk(keys, request = {}) {
        const { token } = request;
        const path = RouteBuilder.historyBatchDelete();
        const url = this.sdk.getUrl(path);
        const result = await this.sdk.http.post(url.toString(), { token, body: JSON.stringify(keys) });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to delete history. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
        if (!result.body) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
        }
        if (!Array.isArray(data.items)) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
        }
        return data;
    }
    /**
     * Reads a history definition from the store.
     * @param key The history key
     * @param request Optional request options.
     * @returns The history object
     */
    async read(key, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.historyItem(key));
        const result = await this.sdk.http.get(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to read a history. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
        if (!result.body) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
        }
        if (data.kind !== HttpHistoryKind) {
            throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
        }
        return data;
    }
    async clearOrganization(organizationKey, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.historyClear('organization', organizationKey));
        const result = await this.sdk.http.delete(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to clear history for the organization. ';
        if (result.status !== 204) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
    }
    async clearProject(organizationKey, projectKey, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.historyClear('project', organizationKey, projectKey));
        const result = await this.sdk.http.delete(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to clear history for the project. ';
        if (result.status !== 204) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
    }
    async clearRequest(organizationKey, projectKey, requestKey, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.historyClear('request', organizationKey, projectKey, requestKey));
        const result = await this.sdk.http.delete(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to clear history for the request. ';
        if (result.status !== 204) {
            this.logInvalidResponse(result);
            let e = this.createGenericSdkError(result.body);
            if (!e) {
                e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
                e.response = result.body;
            }
            throw e;
        }
    }
}
//# sourceMappingURL=HistorySdk.js.map