export type ArchiveView = 'active' | 'archived';
export type OccupancyView = 'active' | 'all' | 'archived';
export type BatchAction = 'archive' | 'restore' | 'checkout';

export interface ViewPolicy {
  batchAction: BatchAction;
  readonly: boolean;
}

export const selectArchiveRecords = <T extends { status?: string }>(
  records: T[],
  view: ArchiveView,
) =>
  records.filter((record) =>
    view === 'archived' ? record.status === 'archived' : record.status !== 'archived',
  );

export const expenseStatusForView = (view: ArchiveView) => view;

export const archiveViewPolicy = (view: ArchiveView): ViewPolicy => ({
  batchAction: view === 'archived' ? 'restore' : 'archive',
  readonly: view === 'archived',
});

export const occupancyViewPolicy = (view: OccupancyView): ViewPolicy => ({
  batchAction: view === 'active' ? 'checkout' : view === 'all' ? 'archive' : 'restore',
  readonly: view === 'archived',
});

export const shouldClearSelectionOnViewChange = <T extends string>(current: T, next: T) =>
  current !== next;

export const occupancyParamsForView = (view: OccupancyView) => ({
  active: view === 'active' ? 'true' : undefined,
  status: view === 'archived' ? 'archived' : 'active',
});
