import { nanoid } from 'nanoid';
import { CertificateFileKind } from './kinds.js';
import { File } from './store/File.js';
import { Thing } from './Thing.js';
export class CertificateFile extends File {
    kind = CertificateFileKind;
    static fromCertificate(certificate, organization, owner) {
        const init = {
            kind: CertificateFileKind,
            key: certificate.key,
            organization,
            info: Thing.fromName(certificate.name).toJSON(),
            lastModified: { user: owner || '', time: 0, byMe: false },
            owner,
            parents: [],
            permissionIds: [],
            permissions: [],
        };
        return new CertificateFile(init);
    }
    /**
     * @param name The name to set.
     * @param owner The user id that is the owner of the file.
     */
    static fromName(name, organization, owner) {
        return super.fromName(name, organization, owner, CertificateFileKind);
    }
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: CertificateFileKind,
                key: nanoid(),
                organization: '',
                info: Thing.fromName('').toJSON(),
                parents: [],
                permissionIds: [],
                permissions: [],
                lastModified: { user: '', time: 0, byMe: false },
            };
        }
        this.new(init);
    }
    new(init) {
        if (!CertificateFile.isCertificate(init)) {
            throw new Error(`Not a certificate file.`);
        }
        super.new(init);
        this.kind = CertificateFileKind;
        return this;
    }
    static isCertificate(input) {
        const typed = input;
        if (!input || typed.kind !== CertificateFileKind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: CertificateFileKind,
        };
        return result;
    }
}
//# sourceMappingURL=CertificateFile.js.map