import { nanoid } from '../nanoid.js';
import { File } from './store/File.js';
import { Thing } from './Thing.js';
import { ProjectKind } from './kinds.js';
/**
 * This model represents a meta data for an HTTP project stored with the data store.
 * This does not include the HTTP project entity, though, it is referenced through the same key.
 */
export class Project extends File {
    kind = ProjectKind;
    static fromProject(project) {
        let final;
        if (typeof project.toJSON === 'function') {
            final = project.toJSON();
        }
        else {
            final = project;
        }
        const init = {
            kind: ProjectKind,
            key: project.key,
            info: { ...final.info },
            lastModified: { user: '', time: 0, byMe: false },
            parents: [],
            permissionIds: [],
            permissions: [],
        };
        return new Project(init);
    }
    /**
     * @param name The name to set.
     */
    static fromName(name) {
        return super.fromName(name, ProjectKind);
    }
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: ProjectKind,
                key: nanoid(),
                info: Thing.fromName('').toJSON(),
                parents: [],
                permissionIds: [],
                permissions: [],
                lastModified: { user: '', time: 0, byMe: false },
            };
        }
        this.new(init);
    }
    new(init) {
        if (!Project.isProject(init)) {
            throw new Error(`Not a project file.`);
        }
        super.new(init);
        this.kind = ProjectKind;
        return this;
    }
    static isProject(input) {
        const typed = input;
        if (!input || typed.kind !== ProjectKind) {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: ProjectKind,
        };
        return result;
    }
}
//# sourceMappingURL=Project.js.map