import { nanoid } from 'nanoid';
import { File } from '../models/store/File.js';
import { Thing } from '../models/Thing.js';
import { DomainFileKind } from '../models/kinds.js';
/**
 * Used by the store. A file definition for the DataDomain
 */
export class DomainFile extends File {
    kind = DomainFileKind;
    static fromName(name, organization, owner) {
        return super.fromName(name, organization, owner, DomainFileKind);
    }
    /**
     * Creates the file definition for a DataNamespace contents.
     *
     * @param input The data namespace instance or schema.
     * @param owner Optional owner to set.
     */
    static fromDataDomain(input, organization, owner) {
        let final;
        if (typeof input.toJSON === 'function') {
            final = input.toJSON();
        }
        else {
            final = input;
        }
        const init = {
            kind: DomainFileKind,
            key: final.key,
            organization,
            info: { ...final.info },
            lastModified: { user: owner || '', time: 0, byMe: false },
            owner,
            parents: [],
            permissionIds: [],
            permissions: [],
        };
        return new DomainFile(init);
    }
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: DomainFileKind,
                key: nanoid(),
                organization: '',
                info: Thing.fromName('').toJSON(),
                parents: [],
                permissionIds: [],
                permissions: [],
                lastModified: { user: '', time: 0, byMe: false },
            };
        }
        this.new(init);
    }
    new(init) {
        if (!DomainFile.isDomainFile(init)) {
            throw new Error(`Not a data file.`);
        }
        super.new(init);
        this.kind = DomainFileKind;
        return this;
    }
    static isDomainFile(input) {
        const typed = input;
        if (!input || typed.kind !== DomainFileKind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: DomainFileKind,
        };
        return result;
    }
}
//# sourceMappingURL=DomainFile.js.map