import { Observable, attr } from '@ni/fast-element';
import { TableColumnSortOperation } from '../base/types';
// As the returned class is internal to the function, we can't write a signature that uses is directly, so rely on inference
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/explicit-function-return-type
export function mixinCustomSortOrderColumnAPI(base) {
    /**
     * The Mixin that provides a concrete column with the API to allow sorting
     * by a field other than the default for that column.
     */
    class CustomSortOrderColumn extends base {
        constructor() {
            super(...arguments);
            /** @internal */
            this.customSortOrderColumnChangeHandler = {
                handleChange: this.handleCustomSortOrderColumnChange.bind(this)
            };
        }
        getResolvedOperandDataRecordFieldName(initialOperandFieldName) {
            return typeof this.sortByFieldName === 'string'
                ? this.sortByFieldName
                : initialOperandFieldName;
        }
        getResolvedSortOperation(initialSortOperation) {
            return typeof this.sortByFieldName === 'string'
                ? TableColumnSortOperation.basic
                : initialSortOperation;
        }
        /** @internal */
        sortByFieldNameChanged() {
            this.handleSortConfigurationChange();
            this.updateCustomColumnSortingValidity();
            if (typeof this.sortByFieldName === 'string'
                && !this.customSortOrderColumnNotifier) {
                this.customSortOrderColumnNotifier = Observable.getNotifier(this.columnInternals);
                this.customSortOrderColumnNotifier.subscribe(this.customSortOrderColumnChangeHandler);
            }
            else {
                this.customSortOrderColumnNotifier?.unsubscribe(this.customSortOrderColumnChangeHandler);
                this.customSortOrderColumnNotifier = undefined;
            }
        }
        /** @internal */
        handleCustomSortOrderColumnChange(_source, args) {
            if (args === 'groupingDisabled') {
                this.updateCustomColumnSortingValidity();
            }
        }
        /** @internal */
        updateCustomColumnSortingValidity() {
            const hasCustomColumnSorting = typeof this.sortByFieldName === 'string';
            const isGroupingEnabled = !this.columnInternals.groupingDisabled;
            const isValid = !isGroupingEnabled || !hasCustomColumnSorting;
            if (isValid) {
                this.columnInternals.validator.untrack('invalidCustomSortWithGrouping');
            }
            else {
                this.columnInternals.validator.track('invalidCustomSortWithGrouping');
            }
        }
    }
    attr({ attribute: 'sort-by-field-name' })(CustomSortOrderColumn.prototype, 'sortByFieldName');
    return CustomSortOrderColumn;
}
//# sourceMappingURL=custom-sort-order.js.map