import { Model } from '../core/Model.js';
import { exponentialBackoffDelay, shouldRetryApiError, retry } from './retry_logic.js';
export default class UserModel extends Model {
    /**
     * The user ID this model is associated with.
     * Note, this value must be set before calling `fetch()`.
     */
    uid;
    /**
     * The organization ID this model is associated with.
     */
    oid;
    /**
     * A reference to the SDK service used to perform API calls.
     */
    sdk;
    /**
     * Whether the model is currently loading data.
     */
    loading = false;
    constructor(sdk, oid) {
        super();
        this.sdk = sdk;
        this.oid = oid;
    }
    async fetch() {
        const { uid, oid } = this;
        if (!uid) {
            throw new Error('User ID (uid) is not set. Cannot fetch user data.');
        }
        this.loading = true;
        const operation = async () => {
            return this.sdk.organizations.users.read(oid, uid);
        };
        const retryOptions = {
            retries: this.retryCount,
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
        };
        try {
            this.raw = await retry(operation, retryOptions);
        }
        finally {
            this.loading = false;
        }
        return this.raw;
    }
    isActive() {
        const { raw } = this;
        if (!raw) {
            throw new Error('User data is not loaded. Cannot determine if user is active.');
        }
        return raw.status === 'active';
    }
    async deactivate() {
        const { uid, oid } = this;
        if (!uid) {
            throw new Error('User ID (uid) is not set. Cannot deactivate user.');
        }
        this.loading = true;
        const operation = async () => {
            return this.sdk.organizations.users.deactivate(oid, uid);
        };
        const retryOptions = {
            retries: this.retryCount,
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
        };
        try {
            this.raw = await retry(operation, retryOptions);
        }
        finally {
            this.loading = false;
        }
    }
    async activate() {
        const { uid, oid } = this;
        if (!uid) {
            throw new Error('User ID (uid) is not set. Cannot activate user.');
        }
        this.loading = true;
        const operation = async () => {
            return this.sdk.organizations.users.activate(oid, uid);
        };
        const retryOptions = {
            retries: this.retryCount,
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
        };
        try {
            this.raw = await retry(operation, retryOptions);
        }
        finally {
            this.loading = false;
        }
    }
}
//# sourceMappingURL=UserModel.js.map