import { TaskStatus } from "@lit/task";
import { customElement } from "lit/decorators.js";
import { afterEach, beforeEach, describe, vi } from "vitest";
import { test as baseTest } from "../../../../test/useMSW.js";
import { EFVideo } from "../../EFVideo";
import { makeVideoInitSegmentFetchTask } from "./makeVideoInitSegmentFetchTask";

@customElement("test-media-video-init-segment-fetch")
class TestMediaVideoInitSegmentFetch extends EFVideo {}

declare global {
  interface HTMLElementTagNameMap {
    "test-media-video-init-segment-fetch": TestMediaVideoInitSegmentFetch;
  }
}

const test = baseTest.extend<{
  element: TestMediaVideoInitSegmentFetch;
}>({
  element: async ({}, use) => {
    const element = document.createElement(
      "test-media-video-init-segment-fetch",
    );
    await use(element);
    element.remove();
  },
});

describe("makeVideoInitSegmentFetchTask", () => {
  beforeEach(() => {
    // MSW setup is now handled by test fixtures
  });

  afterEach(() => {
    const elements = document.querySelectorAll(
      "test-media-video-init-segment-fetch",
    );
    for (const element of elements) {
      element.remove();
    }
    vi.restoreAllMocks();
  });

  test("creates task with correct initial state", ({ element, expect }) => {
    const task = makeVideoInitSegmentFetchTask(element);

    expect(task).toBeDefined();
    expect(task.status).toBe(TaskStatus.INITIAL);
    expect(task.value).toBeUndefined();
    expect(task.error).toBeUndefined();
  });

  test("task integrates with element properties", ({ element, expect }) => {
    const task = makeVideoInitSegmentFetchTask(element);

    expect(task).toBeDefined();
    expect(task.status).toBe(TaskStatus.INITIAL);
  });
});
