import Authorization from "./Authorization";

export default class Account  {

    private _username : string;
    private _salt : Uint8Array;
    private _hash : Uint8Array;
    private _authorizationSet : Array<Authorization>;
    
    constructor(username : string, salt: Uint8Array, hash : Uint8Array) {
        if (username === null && username === undefined)  {
            throw new Error("cannot create Account with null or undefined");
        }
        this._username = username;
        if (salt === null && salt === undefined)  {
            throw new Error("cannot create Account with null or undefined");
        }
        this._salt = salt;
        if (hash === null && hash === undefined)  {
            throw new Error("cannot create Account with null or undefined");
        }
        this._hash = hash;
        this._authorizationSet = [];
    }

    get username() : string {
        return this._username;
    }

    get salt() : Uint8Array {
        return this._salt;
    }

    get hash() : Uint8Array {
        return this._hash;
    }

    get authorizationSet() : Array<Authorization> {
        return [...this._authorizationSet];
    }
    
    addAuthorization(newAuthorization : Authorization) : void{
        if (!this._authorizationSet.find(authorization => authorization.key === newAuthorization.key)) {
            this._authorizationSet.push(newAuthorization);
        }
    }

    removeAuthorization(existingAuthorization : Authorization) : void{
        let index = this._authorizationSet.findIndex(authorization => {
            let sameKey = (authorization.key === existingAuthorization.key);
            let sameKind = (authorization.kind === existingAuthorization.kind);
            return sameKey && sameKind;
        });
        if (index !== -1) {
            this._authorizationSet.splice(index, 1);
        }
    }

}