import { nanoid } from '../nanoid.js';
/**
 * Represents an auth data stored in API Client's data store to be retrieved
 * when the response status is 401.
 *
 * Each entry represent a Basic or NTLM authorization.
 */
export class AuthorizationData {
    key = '';
    username;
    password;
    domain;
    /**
     * @param input The provider definition used to restore the state.
     */
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                key: nanoid(),
            };
        }
        this.new(init);
    }
    /**
     * Creates a new provider clearing anything that is so far defined.
     *
     * Note, this throws an error when the provider is not an API Client provider object.
     */
    new(init) {
        const { username, password, domain, key = nanoid() } = init;
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.key = key;
    }
    toJSON() {
        const result = {
            key: this.key,
        };
        if (this.username) {
            result.username = this.username;
        }
        if (this.password) {
            result.password = this.password;
        }
        if (this.domain) {
            result.domain = this.domain;
        }
        return result;
    }
}
//# sourceMappingURL=AuthorizationData.js.map