/**
 * Internal helpers that translate a column id using dot notation
 * (e.g. `'boundedContext.name'`) into the Sequelize syntax expected by the
 * backend repository:
 *
 * - `where` predicates: `'$assoc.col$'` (dollar-wrapped) — required by
 *   Sequelize to reference a column on an `include`d association.
 * - `order` entries: `['assoc', 'col', dir]` — the array form Sequelize
 *   accepts for ordering by an associated model's column.
 *
 * In Aurora, a column id with a dot has exactly one meaning: "field of an
 * included relation." This convention lets the data-table builders
 * (`buildSearchWhere`, `buildFilterWhere`, `buildSortOrder`) emit the right
 * Sequelize shape without the consumer plumbing it manually.
 *
 * Both helpers are idempotent: a key already wrapped in `$…$` is left as-is,
 * and an order entry already split into 3+ parts is preserved.
 */

export function toSequelizeWhereKey(columnId: string): string {
  if (!columnId.includes('.')) return columnId;
  if (columnId.startsWith('$') && columnId.endsWith('$')) return columnId;
  return `$${columnId}$`;
}

export function toSequelizeOrderEntry(entry: string[]): string[] {
  if (entry.length !== 2) return entry;
  const [field, direction] = entry;
  return field.includes('.') ? [...field.split('.'), direction] : entry;
}
