import type { QueryFilter, SingleFilter } from "@brevity-builder/models";
import { act, renderHook } from "@testing-library/react";
import { expect, it, mock } from "bun:test";
import { useDynamicFilter } from "./useDynamicFilter";

it("remove legacy", () => {
  const legacy: QueryFilter = {
    id: "XMCifC3weeLQAJy3NynfD",
    field: {
      __type: "column",
      id: "name_nGM7N",
      key: "passenger_qEjXQ:name_nGM7N",
      name: "Name",
      type: "text",
      operators: [
        "eq",
        "neq",
        "gt",
        "lt",
        "gte",
        "lte",
        "like",
        "ilike",
        "in",
        "not_in",
        "null",
        "not_null",
      ],
    } as any,
    value: undefined,
    children: [
      {
        id: "ewwaEpMppmJPBGyyJqdw3",
        field: {
          __type: "column",
          id: "destination_dDBzx",
          key: "passenger_qEjXQ:destination_dDBzx",
          name: "Destination",
          type: "text",
          operators: [
            "eq",
            "neq",
            "gt",
            "lt",
            "gte",
            "lte",
            "like",
            "ilike",
            "in",
            "not_in",
            "null",
            "not_null",
          ],
        } as any,
        value: undefined,
        children: [],
        operator: "eq",
        ignoreEmpty: true,
        logicalOperator: "AND",
      },
    ],
    operator: "eq",
    ignoreEmpty: true,
    logicalOperator: "AND",
  };

  const filter: SingleFilter = {
    __type: "filter",
    id: "ewwaEpMppmJPBGyyJqdw3",
    field: {
      __type: "column",
      id: "destination_dDBzx",
      key: "passenger_qEjXQ:destination_dDBzx",
      name: "Destination",
      type: "text",
      operators: [
        "eq",
        "neq",
        "gt",
        "lt",
        "gte",
        "lte",
        "like",
        "ilike",
        "in",
        "not_in",
        "null",
        "not_null",
      ],
    } as any,
    value: undefined,
    operator: "eq",
    ignoreEmpty: true,
  };

  const onChange = mock((group) => {
    console.log("group", group);
  });
  const { result } = renderHook(() => useDynamicFilter(legacy, onChange));
  expect(result.current[0]).toMatchInlineSnapshot(`
[
  {
    "field": {
      "__type": "column",
      "id": "name_nGM7N",
      "key": "passenger_qEjXQ:name_nGM7N",
      "name": "Name",
      "operators": [
        "eq",
        "neq",
        "gt",
        "lt",
        "gte",
        "lte",
        "like",
        "ilike",
        "in",
        "not_in",
        "null",
        "not_null",
      ],
      "type": "text",
    },
    "id": "XMCifC3weeLQAJy3NynfD",
    "ignoreEmpty": true,
    "operator": "eq",
    "value": undefined,
  },
  {
    "__type": "filter",
    "field": {
      "__type": "column",
      "id": "destination_dDBzx",
      "key": "passenger_qEjXQ:destination_dDBzx",
      "name": "Destination",
      "operators": [
        "eq",
        "neq",
        "gt",
        "lt",
        "gte",
        "lte",
        "like",
        "ilike",
        "in",
        "not_in",
        "null",
        "not_null",
      ],
      "type": "text",
    },
    "id": "ewwaEpMppmJPBGyyJqdw3",
    "ignoreEmpty": true,
    "operator": "eq",
    "value": undefined,
  },
]
`);
  act(() => {
    result.current[2].remove(filter);
  });
  expect(onChange).toHaveBeenCalledTimes(1);
  expect(result.current[0]).toMatchInlineSnapshot(`
[
  {
    "field": {
      "__type": "column",
      "id": "name_nGM7N",
      "key": "passenger_qEjXQ:name_nGM7N",
      "name": "Name",
      "operators": [
        "eq",
        "neq",
        "gt",
        "lt",
        "gte",
        "lte",
        "like",
        "ilike",
        "in",
        "not_in",
        "null",
        "not_null",
      ],
      "type": "text",
    },
    "id": "XMCifC3weeLQAJy3NynfD",
    "ignoreEmpty": true,
    "operator": "eq",
    "value": undefined,
  },
]
`);
});
