import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, } from './SdkBase.js';
import { RouteBuilder } from './RouteBuilder.js';
export class UsersSdk extends SdkBase {
    /**
     * Reads the current user.
     * @param request Optional request options.
     */
    async me(request = {}) {
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.usersMe());
        const result = await this.sdk.http.get(url.toString(), { token });
        this.inspectCommonStatusCodes(result);
        const E_PREFIX = 'Unable to read a user. ';
        if (result.status !== 200) {
            this.logInvalidResponse(result);
            throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
        }
        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;
    }
    /**
     * Lists users in the store
     *
     * @param organizationId The key of the organization we want to read the user from.
     * @param options Optional query options.
     * @param request Optional request options.
     * @deprecated Use `organizations.listUsers()` instead.
     */
    async list(organizationId, options, request = {}) {
        // eslint-disable-next-line no-console
        console.warn('The `users.list` method is deprecated. Use `organizations.listUsers()` instead.');
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.users(organizationId));
        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 new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
        }
        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 user information from the store.
     *
     * @param organizationId The key of the organization we want to read the user from.
     * @param key The user key.
     * @param request Optional request options.
     * @returns The user object
     * @deprecated Use `organizations.getUser()` instead.
     */
    async read(organizationId, key, request = {}) {
        // eslint-disable-next-line no-console
        console.warn('The `users.read` method is deprecated. Use `organizations.getUser()` instead.');
        const { token } = request;
        const url = this.sdk.getUrl(RouteBuilder.user(organizationId, 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 new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
        }
        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}.`);
        }
        return data;
    }
}
//# sourceMappingURL=UsersSdk.js.map