import { File } from './store/File.js';
import { nanoid } from '../nanoid.js';
import { Thing } from './Thing.js';
import { FolderKind } from './kinds.js';
/**
 * A definition of a folder.
 */
export class Folder extends File {
    kind = FolderKind;
    /**
     * Creates a new Folder object from a name.
     *
     * @param name The name to set.
     */
    static fromName(name) {
        return super.fromName(name, FolderKind);
    }
    /**
     * @param input The environment definition used to restore the state.
     */
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: FolderKind,
                key: nanoid(),
                info: Thing.fromName('').toJSON(),
                parents: [],
                permissionIds: [],
                permissions: [],
                lastModified: { user: '', time: 0, byMe: false },
            };
        }
        this.new(init);
    }
    /**
     * Creates a new environment clearing anything that is so far defined.
     */
    new(init) {
        if (!Folder.isFolder(init)) {
            throw new Error(`Not a folder.`);
        }
        super.new(init);
        this.kind = FolderKind;
        return this;
    }
    /**
     * Checks whether the input is a definition of a folder.
     */
    static isFolder(input) {
        const typed = input;
        if (!input || typed.kind !== FolderKind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: FolderKind,
        };
        return result;
    }
}
//# sourceMappingURL=Folder.js.map