import { __decorate } from "tslib";
import { observable } from '@ni/fast-element';
import { TableGroupHeaderView } from '../../base/group-header-view';
import { tableGroupRowPlaceholderEmptyLabel, tableGroupRowPlaceholderNoValueLabel } from '../../../label-provider/table/label-tokens';
/**
 * The group header view base class for displaying fields of any type as text.
 */
export class TableColumnTextGroupHeaderViewBase extends TableGroupHeaderView {
    constructor() {
        super(...arguments);
        /** @internal */
        this.hasOverflow = false;
        /**
         * Text to render in the cell.
         *
         * The value is initialized to `tableGroupRowPlaceholderNoValue` because if the group
         * row never has a value defined on it, the change handlers may never get called but
         * the text needs to be correct.
         */
        this.text = tableGroupRowPlaceholderNoValueLabel.getValueFor(this);
        this.noValuePlaceholderLabelSubscriber = {
            handleChange: () => {
                this.applyPlaceholderTextIfNeeded();
            }
        };
        this.emptyPlaceholderLabelSubscriber = {
            handleChange: () => {
                this.applyPlaceholderTextIfNeeded();
            }
        };
    }
    connectedCallback() {
        super.connectedCallback();
        tableGroupRowPlaceholderNoValueLabel.subscribe(this.noValuePlaceholderLabelSubscriber, this);
        tableGroupRowPlaceholderEmptyLabel.subscribe(this.emptyPlaceholderLabelSubscriber, this);
        this.applyPlaceholderTextIfNeeded();
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        tableGroupRowPlaceholderNoValueLabel.unsubscribe(this.noValuePlaceholderLabelSubscriber);
        tableGroupRowPlaceholderEmptyLabel.unsubscribe(this.emptyPlaceholderLabelSubscriber);
    }
    columnConfigChanged() {
        if (!this.applyPlaceholderTextIfNeeded()) {
            this.updateText();
        }
    }
    groupHeaderValueChanged() {
        if (!this.applyPlaceholderTextIfNeeded()) {
            this.updateText();
        }
    }
    /**
     * Sets `this.text` to the appropriate placeholder if `groupHeaderValue` warrants it.
     * @returns `true` if `this.text` was set to a placeholder, `false` otherwise.
     */
    applyPlaceholderTextIfNeeded() {
        if (this.groupHeaderValue === null
            || this.groupHeaderValue === undefined) {
            this.text = tableGroupRowPlaceholderNoValueLabel.getValueFor(this);
            return true;
        }
        if (this.groupHeaderValue === '') {
            this.text = tableGroupRowPlaceholderEmptyLabel.getValueFor(this);
            return true;
        }
        return false;
    }
}
__decorate([
    observable
], TableColumnTextGroupHeaderViewBase.prototype, "hasOverflow", void 0);
__decorate([
    observable
], TableColumnTextGroupHeaderViewBase.prototype, "text", void 0);
//# sourceMappingURL=index.js.map