import { describe, it, expect } from 'vitest';
import { scoreRelevance, sortByRelevance, sortByColumn, toggleSort } from '../../lib/search.js';
describe('scoreRelevance', () => {
    it('returns 100 for exact match (case-insensitive)', () => {
        expect(scoreRelevance('Hello', 'hello')).toBe(100);
        expect(scoreRelevance('test', 'test')).toBe(100);
    });
    it('returns 80 when text starts with the query', () => {
        expect(scoreRelevance('hello world', 'hel')).toBe(80);
        expect(scoreRelevance('Alpha', 'al')).toBe(80);
    });
    it('returns 60 when query matches at a word boundary but not at start', () => {
        expect(scoreRelevance('say hello', 'hello')).toBe(60);
    });
    it('returns 40 when query appears only as a substring without word-boundary match', () => {
        expect(scoreRelevance('axxhello', 'hello')).toBe(40);
    });
    it('returns 0 when there is no match', () => {
        expect(scoreRelevance('abc def', 'xyz')).toBe(0);
    });
    it('returns 0 for an empty query', () => {
        expect(scoreRelevance('anything', '')).toBe(0);
    });
});
describe('sortByRelevance', () => {
    it('sorts items by best field relevance score descending', () => {
        const items = [
            { id: 'a', title: 'zzz' },
            { id: 'b', title: 'hello' },
            { id: 'c', title: 'say hello' },
        ];
        const sorted = sortByRelevance(items, 'hello', (item) => [item.title]);
        // exact match > word-boundary match > no match
        expect(sorted.map((i) => i.id)).toEqual(['b', 'c', 'a']);
    });
    it('returns the original array reference for an empty query', () => {
        const items = [{ id: '1' }, { id: '2' }];
        const result = sortByRelevance(items, '', (item) => [item.id]);
        expect(result).toBe(items);
    });
    it('returns the original array when query is whitespace-only', () => {
        const items = [{ id: '1' }];
        expect(sortByRelevance(items, '   ', (item) => [item.id])).toBe(items);
    });
    it('uses the maximum score across multiple search fields', () => {
        const items = [
            { id: 'low', a: 'x', b: 'x' },
            { id: 'high', a: 'x', b: 'exact' },
        ];
        const sorted = sortByRelevance(items, 'exact', (item) => [item.a, item.b]);
        expect(sorted[0].id).toBe('high');
    });
});
describe('sortByColumn', () => {
    it('returns the original array when sortConfig is null', () => {
        const items = [{ n: 2 }, { n: 1 }];
        expect(sortByColumn(items, null, (item, key) => item[key])).toBe(items);
    });
    it('sorts strings ascending', () => {
        const items = [{ name: 'b' }, { name: 'a' }, { name: 'c' }];
        const sorted = sortByColumn(items, { key: 'name', direction: 'asc' }, (item, key) => item[key]);
        expect(sorted.map((i) => i.name)).toEqual(['a', 'b', 'c']);
    });
    it('sorts strings descending', () => {
        const items = [{ name: 'a' }, { name: 'c' }, { name: 'b' }];
        const sorted = sortByColumn(items, { key: 'name', direction: 'desc' }, (item, key) => item[key]);
        expect(sorted.map((i) => i.name)).toEqual(['c', 'b', 'a']);
    });
    it('sorts numbers ascending and descending', () => {
        const items = [{ v: 3 }, { v: 1 }, { v: 2 }];
        const asc = sortByColumn(items, { key: 'v', direction: 'asc' }, (item, key) => item[key]);
        expect(asc.map((i) => i.v)).toEqual([1, 2, 3]);
        const desc = sortByColumn(items, { key: 'v', direction: 'desc' }, (item, key) => item[key]);
        expect(desc.map((i) => i.v)).toEqual([3, 2, 1]);
    });
});
describe('toggleSort', () => {
    it('returns ascending sort when current is null', () => {
        expect(toggleSort(null, 'title')).toEqual({ key: 'title', direction: 'asc' });
    });
    it('toggles same key from asc to desc', () => {
        expect(toggleSort({ key: 'title', direction: 'asc' }, 'title')).toEqual({
            key: 'title',
            direction: 'desc',
        });
    });
    it('toggles same key from desc to asc', () => {
        expect(toggleSort({ key: 'title', direction: 'desc' }, 'title')).toEqual({
            key: 'title',
            direction: 'asc',
        });
    });
    it('starts ascending when switching to a different key', () => {
        expect(toggleSort({ key: 'foo', direction: 'desc' }, 'bar')).toEqual({
            key: 'bar',
            direction: 'asc',
        });
    });
});
//# sourceMappingURL=search.test.js.map