import { FindOptions } from "sequelize";

// orderItem puede ser: ['campo', 'ASC'|'DESC']
// o [Sequelize.literal('score'), 'DESC']
// o [[{ model: X, as: 'alias' }, 'campo', 'ASC']] etc.
type OrderItem = any;

export function prependOrder(options: FindOptions, orderItem: OrderItem) {
  if (!options.order) {
    options.order = [orderItem];
    return;
  }
  // Si viene como algo no-array (poco común), lo envolvemos
  if (!Array.isArray(options.order)) {
    options.order = [options.order as any];
  }
  (options.order as OrderItem[]).unshift(orderItem);
}

export function appendOrder(options: FindOptions, orderItem: OrderItem) {
  if (!options.order) {
    options.order = [orderItem];
    return;
  }
  if (!Array.isArray(options.order)) {
    options.order = [options.order as any];
  }
  (options.order as OrderItem[]).push(orderItem);
}
