import { nanoid } from 'nanoid';
export const Kind = 'Core#Organization';
export class Organization {
    kind = Kind;
    key = '';
    name = '';
    createdBy = '';
    createdDate = 0;
    deleted = false;
    deletedInfo;
    grantType = 'member';
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: Kind,
                key: nanoid(),
                name: '',
                createdBy: '',
                createdDate: Date.now(),
                deleted: false,
                grantType: 'member',
            };
        }
        this.new(init);
    }
    new(init) {
        if (!Organization.isOrganization(init)) {
            throw new Error(`Not an Organization.`);
        }
        const { key = nanoid(), name, createdBy, createdDate = Date.now(), deleted = false, deletedInfo, grantType = 'member', } = init;
        this.kind = Kind;
        this.key = key;
        this.name = name;
        this.createdBy = createdBy;
        this.createdDate = createdDate;
        this.deleted = deleted;
        this.grantType = grantType;
        if (deletedInfo) {
            this.deletedInfo = deletedInfo;
        }
        else {
            this.deletedInfo = undefined;
        }
    }
    /**
     * Checks whether the input is a definition of an user organization.
     */
    static isOrganization(input) {
        const typed = input;
        if (!input || typed.kind !== Kind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            kind: Kind,
            key: this.key,
            name: this.name,
            createdBy: this.createdBy,
            createdDate: this.createdDate,
            deleted: this.deleted,
            grantType: this.grantType,
        };
        if (this.deletedInfo) {
            result.deletedInfo = this.deletedInfo;
        }
        return result;
    }
}
//# sourceMappingURL=Organization.js.map