import { ThingKind } from './kinds.js';
export class Thing {
    kind = ThingKind;
    /**
     * The name of the thing.
     */
    name;
    /**
     * Optional value to overwrite the `name` in the UI.
     * The primary descriptive field is the `name`. The display name is only used in the presentation of the data.
     */
    displayName;
    /**
     * The description of the thing.
     */
    description;
    /**
     * The version number of the thing.
     */
    version;
    /**
     * Returns one in this order:
     * - displayName
     * - name
     * - 'Unnamed object'
     */
    get renderLabel() {
        return this.displayName || this.name || 'Unnamed object';
    }
    /**
     * Creates a basic description from a name.
     */
    static fromName(name) {
        const thing = new Thing({
            name,
            kind: ThingKind,
        });
        return thing;
    }
    /**
     * @param input The thing definition used to restore the state.
     */
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: ThingKind,
            };
        }
        this.new(init);
    }
    /**
     * Creates a new thing clearing anything that is so far defined.
     *
     * Note, this throws an error when the server is not a thing.
     */
    new(init) {
        if (!Thing.isThing(init)) {
            throw new Error(`Not a thing.`);
        }
        const { description, name, version, displayName } = init;
        this.kind = ThingKind;
        this.name = name;
        this.displayName = displayName;
        this.description = description;
        this.version = version;
    }
    /**
     * Checks whether the input is a definition of a server.
     */
    static isThing(input) {
        const typed = input;
        if (input && typed.kind === ThingKind) {
            return true;
        }
        return false;
    }
    toJSON() {
        const result = {
            kind: ThingKind,
        };
        if (typeof this.name === 'string') {
            result.name = this.name;
        }
        if (typeof this.displayName === 'string') {
            result.displayName = this.displayName;
        }
        if (this.description) {
            result.description = this.description;
        }
        if (this.version) {
            result.version = this.version;
        }
        return result;
    }
}
//# sourceMappingURL=Thing.js.map