import {
    buildAccountFilterOptions,
    getDeletingJobBlockedReason,
    TRANSFER_NOT_VISIBLE_REASON,
} from '../transfer-list';
import type { ProjectTransferJob } from 'src/apollo/queries/transfer-projects';
import type { ListViewItem } from '@theorchard/suite-components';

const searchOption = (name: string, vendorId: number): ListViewItem => ({
    label: name,
    value: `acc-${vendorId}`,
    subtitle: String(vendorId),
});

describe('buildAccountFilterOptions', () => {
    it('keeps "All Accounts" first on the empty search', () => {
        expect(buildAccountFilterOptions([])[0]).toEqual({
            label: 'All Accounts',
            value: '',
        });
    });

    it('omits "All Accounts" when a search term is present', () => {
        const options = buildAccountFilterOptions(
            [searchOption('Acme Records', 101)],
            'acme'
        );
        expect(options).toHaveLength(1);
        expect(options[0].label).toBe('Acme Records');
    });

    it('remaps the vendor id into the value so the jobs filter can match it', () => {
        const [, opt] = buildAccountFilterOptions([
            searchOption('Acme Records', 101),
        ]);
        expect(opt.label).toBe('Acme Records');
        expect(opt.value).toBe('101');
        expect(opt.subtitle).toBe('101');
    });
});

const job = (id: string, status: ProjectTransferJob['status']) =>
    ({ projectTransferJobId: id, status }) as ProjectTransferJob;

describe('getDeletingJobBlockedReason', () => {
    it('returns undefined when no delete is pending', () => {
        expect(
            getDeletingJobBlockedReason([job('t-1', 'QUEUED')], null)
        ).toBeUndefined();
    });

    it('returns undefined while the job is still deletable', () => {
        expect(
            getDeletingJobBlockedReason([job('t-1', 'QUEUED')], 't-1')
        ).toBeUndefined();
    });

    it('blocks when a poll flips the job to PROCESSING', () => {
        expect(
            getDeletingJobBlockedReason([job('t-1', 'PROCESSING')], 't-1')
        ).toBe('Transfer is in progress and cannot be deleted');
    });

    it('blocks when the job is no longer on the current page', () => {
        expect(getDeletingJobBlockedReason([job('t-2', 'QUEUED')], 't-1')).toBe(
            TRANSFER_NOT_VISIBLE_REASON
        );
    });
});
