/**
 * Vega Table Sorter
 * This is a helper class for sorting table data based on defined columns to be sorted
 * Supports multi-column sorting
 */
export class VegaTableSorter {
  constructor(prop, order, sorter) {
    this.prop = prop;
    this.order = order;
    this.sorter = sorter;
  }
  /**
   * Sort utility
   *
   * @param {VegaTableDataSourceItem} a column value
   * @param {VegaTableDataSourceItem} b column value
   * @returns {Nullable<number>} compare result
   */
  sort(a, b) {
    const compareResult = this.sorter(a, b);
    if (compareResult !== 0) {
      return this.order === 'asc' ? compareResult : -compareResult;
    }
  }
  /**
   * Get params from sorter object for external use
   *
   * @returns {VegaTableSorterType} sorter prop and order
   */
  toExternalFormat() {
    return { prop: this.prop, order: this.order };
  }
  /**
   * Get sorter order value
   *
   * @returns {VegaTableSortOrderType} sorter order
   */
  getSortOrder() {
    return this.order;
  }
}
