import { nanoid } from '../../nanoid.js';
import { OrganizationKind } from '../kinds.js';
export class Organization {
    kind = OrganizationKind;
    key = '';
    name = '';
    createdBy = '';
    createdDate = 0;
    deleted = false;
    deletedInfo;
    grantType = 'viewer';
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: OrganizationKind,
                key: nanoid(),
                name: '',
                createdBy: '',
                createdDate: Date.now(),
                deleted: false,
                grantType: 'viewer',
            };
        }
        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 = 'viewer', } = init;
        this.kind = OrganizationKind;
        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 !== OrganizationKind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            kind: OrganizationKind,
            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