import { nanoid } from '../nanoid.js';
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) {
        const init = {
            kind: CertificateFileKind,
            key: certificate.key,
            info: Thing.fromName(certificate.name).toJSON(),
            lastModified: { user: '', time: 0, byMe: false },
            parents: [],
            permissionIds: [],
            permissions: [],
        };
        return new CertificateFile(init);
    }
    /**
     * @param name The name to set.
     */
    static fromName(name) {
        return super.fromName(name, 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(),
                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