import { describe, expect, it } from 'vitest';
import {
  archiveViewPolicy,
  expenseStatusForView,
  occupancyParamsForView,
  occupancyViewPolicy,
  selectArchiveRecords,
  shouldClearSelectionOnViewChange,
} from './archive-view';

describe('归档数据视图', () => {
  it('正常视图与归档视图不会混合记录', () => {
    const records = [
      { id: 1, status: 'active' },
      { id: 2, status: 'graduated' },
      { id: 3, status: 'archived' },
    ];

    expect(selectArchiveRecords(records, 'active').map((item) => item.id)).toEqual([1, 2]);
    expect(selectArchiveRecords(records, 'archived').map((item) => item.id)).toEqual([3]);
  });

  it('费用视图映射为后端 status 查询', () => {
    expect(expenseStatusForView('active')).toBe('active');
    expect(expenseStatusForView('archived')).toBe('archived');
  });

  it('入住三态分别映射为在住、全部活动记录和归档记录', () => {
    expect(occupancyParamsForView('active')).toEqual({ active: 'true', status: 'active' });
    expect(occupancyParamsForView('all')).toEqual({ active: undefined, status: 'active' });
    expect(occupancyParamsForView('archived')).toEqual({
      active: undefined,
      status: 'archived',
    });
  });

  it('正常与归档视图的批量动作互斥，且归档视图只读', () => {
    expect(archiveViewPolicy('active')).toEqual({
      batchAction: 'archive',
      readonly: false,
      purgeBatch: false,
    });
    expect(archiveViewPolicy('archived')).toEqual({
      batchAction: 'restore',
      readonly: true,
      purgeBatch: true,
    });
  });

  it('入住三态分别只提供退宿、归档和恢复动作', () => {
    expect(occupancyViewPolicy('active')).toEqual({
      batchAction: 'checkout',
      readonly: false,
      purgeBatch: false,
    });
    expect(occupancyViewPolicy('all')).toEqual({
      batchAction: 'archive',
      readonly: false,
      purgeBatch: false,
    });
    expect(occupancyViewPolicy('archived')).toEqual({
      batchAction: 'restore',
      readonly: true,
      purgeBatch: true,
    });
  });

  it('批量删除只出现在归档视图，且与批量恢复互斥', () => {
    expect(archiveViewPolicy('active').purgeBatch).toBe(false);
    expect(archiveViewPolicy('archived').purgeBatch).toBe(true);
    expect(occupancyViewPolicy('active').purgeBatch).toBe(false);
    expect(occupancyViewPolicy('all').purgeBatch).toBe(false);
    expect(occupancyViewPolicy('archived').purgeBatch).toBe(true);
    // 归档视图中批量动作固定为恢复，不会同时出现归档；批量删除只在归档视图开启
    expect(archiveViewPolicy('archived').batchAction).toBe('restore');
    expect(occupancyViewPolicy('archived').batchAction).toBe('restore');
  });

  it('只有实际切换视图时才要求清空选择', () => {
    expect(shouldClearSelectionOnViewChange('active', 'archived')).toBe(true);
    expect(shouldClearSelectionOnViewChange('archived', 'archived')).toBe(false);
  });
});
