export class SplitItem {
    manager;
    kind = '';
    key = '';
    parent;
    label = '';
    pinned;
    index = 0;
    icon;
    persistent;
    loading;
    isDirty;
    value;
    /**
     * @param manager A reference to the manager.
     * @param id The id of the item in the layout system. This value is never serialized.
     * @param schema A schema to restore the state of the panel.
     */
    constructor(manager, schema) {
        this.manager = manager;
        this.new(schema);
    }
    new(schema) {
        this.kind = schema.kind;
        this.key = schema.key;
        this.label = schema.label;
        if (schema.parent) {
            this.parent = schema.parent;
        }
        else {
            this.parent = undefined;
        }
        if (schema.icon) {
            this.icon = schema.icon;
        }
        else {
            this.icon = undefined;
        }
        if (typeof schema.pinned === 'boolean') {
            this.pinned = schema.pinned;
        }
        else {
            this.pinned = undefined;
        }
        if (typeof schema.persistent === 'boolean') {
            this.persistent = schema.persistent;
        }
        else {
            this.persistent = undefined;
        }
        if (typeof schema.loading === 'boolean') {
            this.loading = schema.loading;
        }
        else {
            this.loading = undefined;
        }
        if (typeof schema.isDirty === 'boolean') {
            this.isDirty = schema.isDirty;
        }
        else {
            this.isDirty = undefined;
        }
        if (typeof schema.index === 'number') {
            this.index = schema.index;
        }
        else {
            this.index = 0;
        }
        this.value = schema.value;
    }
    toJSON() {
        const result = {
            key: this.key,
            kind: this.kind,
            label: this.label,
        };
        if (this.parent) {
            result.parent = this.parent;
        }
        if (this.icon) {
            result.icon = this.icon;
        }
        if (typeof this.pinned === 'boolean') {
            result.pinned = this.pinned;
        }
        if (typeof this.persistent === 'boolean') {
            result.persistent = this.persistent;
        }
        if (typeof this.loading === 'boolean') {
            result.loading = this.loading;
        }
        if (typeof this.isDirty === 'boolean') {
            result.isDirty = this.isDirty;
        }
        if (typeof this.index === 'number') {
            result.index = this.index;
        }
        if (this.value) {
            // try toJSON if any
            const typed = this.value;
            if (typeof typed.toJSON === 'function') {
                result.value = typed.toJSON();
            }
            else {
                result.value = this.value;
            }
        }
        return result;
    }
    // /**
    //  * @returns The parent of the item. Returns undefined when the item was removed from layout.
    //  * @deprecated Use `getParents()` instead.
    //  */
    // getParent(): SplitPanel | undefined {
    //   return this.manager.getParent(this.key);
    // }
    /**
     * @returns The parent of the item. Returns undefined when the item was removed from layout.
     */
    getParents() {
        return this.manager.getParents(this.key);
    }
    // /**
    //  * Removes self from the layout. This removes the item from all panels.
    //  */
    // remove(): void {
    //   this.manager.removeItem(this.key);
    // }
    // /**
    //  * Sets the item active in the parent layout.
    //  */
    // setSelected(): void {
    //   const parent = this.getParent();
    //   if (parent) {
    //     parent.selected = this.key;
    //     this.notifyChange();
    //   }
    // }
    /**
     * Updates the label and triggers side effects like layout and view update.
     *
     * @param value The new value of the label. Empty values are ignored.
     * @param trigger Whether to trigger side effects (requesting a view update and notifying change)
     */
    setLabel(value, trigger = true) {
        if (!value || this.label === value) {
            return;
        }
        this.label = value;
        if (trigger) {
            this.updateView();
            this.notifyChange();
        }
    }
    updateView() {
        const panels = this.getParents();
        panels.forEach((panel) => panel.updateView());
    }
    notifyChange() {
        this.manager.notifyChange();
    }
}
//# sourceMappingURL=SplitItem.js.map