import { describe, it, expect, vi, beforeEach } from 'vitest';
import { wireFillFromTab, wireHostnameChip } from '../url-tools';

describe('wireFillFromTab', () => {
  let form: HTMLElement;
  let sendMessage: ReturnType<typeof vi.fn>;

  beforeEach(() => {
    form = document.createElement('div');
    form.innerHTML = `
      <input id="f-title" type="text" />
      <div class="inline-row">
        <input id="f-url" type="text" />
        <button id="fill-from-tab-btn" class="glyph-btn" type="button" title="fill from active tab">⤓</button>
      </div>
    `;
    document.body.appendChild(form);
    sendMessage = vi.fn();
  });

  it('fills url + title from active tab on click', async () => {
    sendMessage.mockResolvedValue({ ok: true, data: { url: 'https://github.com/login', title: 'GitHub' } });
    wireFillFromTab(form, { sendMessage });
    (form.querySelector('#fill-from-tab-btn') as HTMLButtonElement).click();
    await Promise.resolve(); await Promise.resolve();
    expect((form.querySelector('#f-url') as HTMLInputElement).value).toBe('https://github.com/login');
    expect((form.querySelector('#f-title') as HTMLInputElement).value).toBe('GitHub');
  });

  it('does not overwrite a non-empty title', async () => {
    (form.querySelector('#f-title') as HTMLInputElement).value = 'My GitHub';
    sendMessage.mockResolvedValue({ ok: true, data: { url: 'https://github.com/login', title: 'GitHub' } });
    wireFillFromTab(form, { sendMessage });
    (form.querySelector('#fill-from-tab-btn') as HTMLButtonElement).click();
    await Promise.resolve(); await Promise.resolve();
    expect((form.querySelector('#f-title') as HTMLInputElement).value).toBe('My GitHub');
  });

  it('disables the button if SW returns null', async () => {
    sendMessage.mockResolvedValue({ ok: true, data: null });
    wireFillFromTab(form, { sendMessage });
    (form.querySelector('#fill-from-tab-btn') as HTMLButtonElement).click();
    await Promise.resolve(); await Promise.resolve();
    expect((form.querySelector('#fill-from-tab-btn') as HTMLButtonElement).disabled).toBe(true);
  });
});

describe('wireHostnameChip', () => {
  let form: HTMLElement;

  beforeEach(() => {
    form = document.createElement('div');
    form.innerHTML = `
      <div class="form-group">
        <input id="f-url" type="text" />
        <div id="hostname-chip-row" class="hostname-chip-row" hidden></div>
      </div>
    `;
    document.body.appendChild(form);
    vi.useFakeTimers();
  });

  it('renders chip + hostname on valid URL after debounce', () => {
    wireHostnameChip(form);
    const input = form.querySelector('#f-url') as HTMLInputElement;
    input.value = 'https://github.com/login';
    input.dispatchEvent(new Event('input'));
    vi.advanceTimersByTime(250);
    const row = form.querySelector('#hostname-chip-row') as HTMLElement;
    expect(row.hidden).toBe(false);
    expect(row.textContent).toContain('github.com');
    expect(row.querySelector('.hostname-chip')?.textContent).toBe('G');
  });

  it('hides chip if URL is empty', () => {
    wireHostnameChip(form);
    const input = form.querySelector('#f-url') as HTMLInputElement;
    input.value = '';
    input.dispatchEvent(new Event('input'));
    vi.advanceTimersByTime(250);
    expect((form.querySelector('#hostname-chip-row') as HTMLElement).hidden).toBe(true);
  });

  it('hides chip if URL does not parse', () => {
    wireHostnameChip(form);
    const input = form.querySelector('#f-url') as HTMLInputElement;
    input.value = '!!!not-a-url';
    input.dispatchEvent(new Event('input'));
    vi.advanceTimersByTime(250);
    expect((form.querySelector('#hostname-chip-row') as HTMLElement).hidden).toBe(true);
  });

  it('treats scheme-less host as https://', () => {
    wireHostnameChip(form);
    const input = form.querySelector('#f-url') as HTMLInputElement;
    input.value = 'gitlab.com/users/sign_in';
    input.dispatchEvent(new Event('input'));
    vi.advanceTimersByTime(250);
    const row = form.querySelector('#hostname-chip-row') as HTMLElement;
    expect(row.hidden).toBe(false);
    expect(row.textContent).toContain('gitlab.com');
  });
});
