import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, } from './SdkBase.js';
import { RouteBuilder } from './RouteBuilder.js';
import { Exception } from '../exceptions/exception.js';
export class OrganizationsSdk extends SdkBase {
    /**
     * Lists all user organizations.
     */
    async list(request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.organizations());
        const result = await this.sdk.http.get(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to list your organizations. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
        }
        if (!result.body) {
            throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
        }
        if (!Array.isArray(data.items)) {
            throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
        }
        return data;
    }
    /**
     * Creates a new organization.
     * @param orgName The name of the organization to create.
     * @param request The request options.
     * @returns A promise that resolves to the created organization.
     */
    async create(orgName, request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.organizations());
        const body = {
            name: orgName,
        };
        const result = await this.sdk.http.post(url.toString(), {
            body: JSON.stringify(body),
            token,
            headers: {
                'Content-Type': 'application/json',
            },
        });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to create an organization. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
        }
        if (!result.body) {
            throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
        }
        let data;
        try {
            data = JSON.parse(result.body);
        }
        catch {
            throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
        }
        if (!data.kind) {
            throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
        }
        return data;
    }
    invitations = {
        /**
         * Lists all invitations for a given organization.
         * @param oid The organization ID.
         * @param request The request options.
         * @returns A promise that resolves to a list of invitations.
         */
        list: async (oid, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.invitations(oid));
            const result = await this.sdk.http.get(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to list organization invitations. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!Array.isArray(data.items)) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Creates an invitation for a user to join an organization.
         * @param oid The organization ID.
         * @param email The email address of the user to invite.
         * @param grant_type The type of grant for the user.
         * @param name The name of the user (optional).
         * @param request The request options.
         * @returns A promise that resolves to the created invitation.
         */
        create: async (oid, email, grant_type, name, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.invitations(oid));
            const body = {
                email,
                name,
                grant_type,
            };
            const result = await this.sdk.http.post(url.toString(), {
                body: JSON.stringify(body),
                token: request.token,
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to create an invitation. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Finds an invitation by its token.
         * @param oid The organization ID.
         * @param token The invitation token.
         * @param request The request options.
         * @returns A promise that resolves to the found invitation.
         */
        findByToken: async (token, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.findInvitation());
            url.searchParams.append('token', token);
            const result = await this.sdk.http.get(url.toString(), request);
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to find invitation by token. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Declines an invitation.
         * @param oid The organization ID.
         * @param id The invitation ID.
         * @param request The request options.
         * @returns A promise that resolves when the invitation is declined.
         */
        decline: async (oid, id, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.declineInvitation(oid, id));
            const result = await this.sdk.http.post(url.toString(), request);
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to decline invitation. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Soft-deletes an invitation.
         * @param oid The organization ID.
         * @param id The invitation ID.
         * @param request The request options.
         * @returns A promise that resolves to the deleted invitation.
         */
        delete: async (oid, id, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.invitation(oid, id));
            const result = await this.sdk.http.delete(url.toString(), request);
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to delete invitation. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Patches an invitation. The server performs the patch validation.
         * The API only allows to patch some of the invitation properties:
         * - name
         * - expires_at
         * - grant_type
         * @param oid The organization ID.
         * @param id The invitation ID.
         * @param info The patch information.
         * @param request The request options.
         * @returns A promise that resolves to the patched invitation.
         */
        patch: async (oid, id, info, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.invitation(oid, id));
            const result = await this.sdk.http.patch(url.toString(), {
                body: JSON.stringify(info),
                token: request.token,
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to update invitation. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Resends an invitation.
         * @param oid The organization ID.
         * @param id The invitation ID.
         * @param request The request options.
         * @returns A promise that resolves to the updated invitation.
         */
        resend: async (oid, id, request = {}) => {
            const url = this.sdk.getUrl(RouteBuilder.resendInvitation(oid, id));
            const result = await this.sdk.http.put(url.toString(), request);
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to resend invitation. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!data.kind) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
    };
    users = {
        /**
         * Lists users in the organization.
         *
         * @param oid The key of the organization we want to read the user from.
         * @param options Optional query options.
         * @param request Optional request options.
         */
        list: async (oid, options, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUsers(oid));
            this.sdk.appendListOptions(url, options);
            const result = await this.sdk.http.get(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to list projects. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!Array.isArray(data.items)) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Gets a user by its key in the organization.
         *
         * @param oid The key of the organization parent organization.
         * @param key The user key.
         * @param request Optional request options.
         * @returns The user object
         */
        read: async (oid, key, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUser(oid, key));
            const result = await this.sdk.http.get(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to read the user info. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            return data;
        },
        /**
         * Reads user objects in the organization by their keys.
         * Note that the API returns only the users that are part of the organization.
         * If a user is not part of the organization, it will not be ignored. It is up to the caller to handle this case.
         * @param oid The key of the organization parent organization.
         * @param eys A list of user keys to read.
         * @param request Optional request options.
         * @returns The created user object.
         */
        readBatch: async (oid, keys, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUserBatch(oid));
            const body = {
                ids: keys,
            };
            const result = await this.sdk.http.post(url.toString(), {
                body: JSON.stringify(body),
                token,
            });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to read the user batch info. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            if (!Array.isArray(data.items)) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_UNKNOWN}`, { code: 'E_RESPONSE_UNKNOWN', status: result.status });
            }
            return data;
        },
        /**
         * Activates a user in the organization.
         * If the user is already active the API will respond with an error.
         * @param oid The key of the organization parent organization.
         * @param key The user key.
         */
        activate: async (oid, key, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUserActivate(oid, key));
            const result = await this.sdk.http.post(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to activate the user. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            return data;
        },
        /**
         * Deactivates a user in the organization.
         * If the user is already deactivated the API will respond with an error.
         * @param oid The key of the organization parent organization.
         * @param key The user key.
         */
        deactivate: async (oid, key, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUserDeactivate(oid, key));
            const result = await this.sdk.http.post(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to deactivate the user. ';
            if (result.status !== 200) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
            if (!result.body) {
                throw new Exception(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, { code: 'E_RESPONSE_NO_VALUE', status: result.status });
            }
            let data;
            try {
                data = JSON.parse(result.body);
            }
            catch {
                throw new Exception(`${E_PREFIX}${E_INVALID_JSON}`, { code: 'E_INVALID_JSON', status: result.status });
            }
            return data;
        },
        /**
         * Deletes a user from the organization.
         * @param oid The key of the organization parent organization.
         * @param key The user
         * @param request Optional request options.
         * @returns A promise that resolves when the user is deleted.
         */
        delete: async (oid, key, request = {}) => {
            const { token } = request;
            const url = this.sdk.getUrl(RouteBuilder.organizationUser(oid, key));
            const result = await this.sdk.http.delete(url.toString(), { token });
            this.inspectCommonStatusCodes(result);
            const E_PREFIX = 'Unable to delete the user. ';
            if (result.status !== 204) {
                this.logInvalidResponse(result);
                throw this.createApiError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.body);
            }
        },
    };
}
//# sourceMappingURL=OrganizationsSdk.js.map