import { ApiBase } from '../Implementation/ApiBase';
import { parseToISO } from '../../Utilities/Helpers/DateHelper';
export class CustomSortInternalApi extends ApiBase {
    getCustomSortComparer(columnId) {
        const column = this.getColumnApi().getColumnWithColumnId(columnId);
        return this.getCustomSortOptions().customSortComparers?.find((csc) => this.getColumnScopeApi().isColumnInScope(column, csc.scope));
    }
    getCustomSortValue(value, columnDataType) {
        if (value == null) {
            return null;
        }
        if (columnDataType === 'date') {
            parseToISO(value);
        }
        if (columnDataType === 'number') {
            return parseFloat(value);
        }
        return `${value}`;
    }
    getDefaultCustomSortComparer(columnId, columnValues) {
        // have to return a function that may not have access to this
        const gridApi = this.getGridApi();
        const columnDataType = this.getColumnApi().getColumnDataTypeForColumnId(columnId);
        const _this = this;
        return function compareItemsOfCustomSort(valueA, valueB, nodeA, nodeB) {
            const firstRawValue = gridApi.getRawValueFromRowNode(nodeA, columnId);
            let firstCustomSortValue = _this.getCustomSortValue(firstRawValue, columnDataType) ?? valueA;
            const secondRawValue = gridApi.getRawValueFromRowNode(nodeB, columnId);
            let secondCustomSortValue = _this.getCustomSortValue(secondRawValue, columnDataType) ?? valueB;
            let indexFirstElement = columnValues.indexOf(firstCustomSortValue);
            let containsFirstElement = indexFirstElement >= 0;
            let indexSecondElement = columnValues.indexOf(secondCustomSortValue);
            let containsSecondElement = indexSecondElement >= 0;
            //if none of the element are in the list we jsut return normal compare
            if (!containsFirstElement && !containsSecondElement) {
                if (valueA == valueB) {
                    return 0;
                }
                return valueA < valueB ? -1 : 1;
            }
            //if first item not in the list make sure we put it after the second item
            if (!containsFirstElement) {
                return 1;
            }
            //if second item not in the list make sure we put it after the first item
            if (!containsSecondElement) {
                return -1;
            }
            //return the comparison from the list if the two items are in the list
            return indexFirstElement - indexSecondElement;
        };
    }
    columnHasCustomSortComparer(columnId) {
        const columnSortComparer = this.getCustomSortComparer(columnId);
        return columnSortComparer ? true : false;
    }
}
