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 { makeVideoSegmentIdTask } from "./makeVideoSegmentIdTask";

@customElement("test-media-video-segment")
class TestMediaVideoSegment extends EFVideo {}

declare global {
  interface HTMLElementTagNameMap {
    "test-media-video-segment": TestMediaVideoSegment;
  }
}

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

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

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

  test("creates task with correct initial state", ({ element, expect }) => {
    const task = makeVideoSegmentIdTask(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 }) => {
    element.desiredSeekTimeMs = 7500;

    const task = makeVideoSegmentIdTask(element);
    expect(task).toBeDefined();
    expect(task.status).toBe(TaskStatus.INITIAL);
  });
});
