import v4 from '../../lib/uuid.js';
import { File } from './File.js';
import { Thing } from '../Thing.js';
import { DataFileKind as Kind } from '../kinds.js';
/**
 * Used by the store. A file definition for the DataNamespace
 */
export class DataFile extends File {
    kind = Kind;
    /**
     * Creates the file definition for a DataNamespace contents.
     *
     * @param input The data namespace instance or schema.
     * @param owner Optional owner to set.
     */
    static fromDataNamespace(input, organization, owner) {
        let final;
        if (typeof input.toJSON === 'function') {
            final = input.toJSON();
        }
        else {
            final = input;
        }
        const init = {
            kind: Kind,
            key: final.key,
            organization,
            info: { ...final.info },
            lastModified: { user: owner || '', time: 0, byMe: false },
            owner,
            parents: [],
            permissionIds: [],
            permissions: [],
        };
        return new DataFile(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, Kind);
    }
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: Kind,
                key: v4(),
                organization: '',
                info: Thing.fromName('').toJSON(),
                parents: [],
                permissionIds: [],
                permissions: [],
                lastModified: { user: '', time: 0, byMe: false },
            };
        }
        this.new(init);
    }
    new(init) {
        if (!DataFile.isDataFile(init)) {
            throw new Error(`Not a data file.`);
        }
        super.new(init);
        this.kind = Kind;
        return this;
    }
    static isDataFile(input) {
        const typed = input;
        if (!input || typed.kind !== Kind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: Kind,
        };
        return result;
    }
}
//# sourceMappingURL=DataFile.js.map