'use strict';

const queryKeyFactory = require('@lukemorales/query-key-factory');

function isErrorResponse(response) {
  return typeof response === "object" && response !== null && "error" in response && response.error !== null && response.error !== void 0;
}
function toError(error) {
  if (error instanceof Error) {
    return error;
  }
  if (typeof error === "object" && error !== null) {
    const errorObj = error;
    const message = (typeof errorObj.message === "string" ? errorObj.message : null) || (typeof errorObj.error === "string" ? errorObj.error : null) || JSON.stringify(error);
    const err = new Error(message);
    Object.assign(err, error);
    return err;
  }
  return new Error(String(error));
}
function createKanbanQueryKeys(client, headers) {
  const boards = createBoardsQueries(client, headers);
  return queryKeyFactory.mergeQueryKeys(boards);
}
function createBoardsQueries(client, headers) {
  return queryKeyFactory.createQueryKeys("boards", {
    list: (params) => ({
      queryKey: [
        {
          slug: params?.slug,
          ownerId: params?.ownerId,
          organizationId: params?.organizationId,
          limit: params?.limit ?? 50,
          offset: params?.offset ?? 0
        }
      ],
      queryFn: async () => {
        try {
          const response = await client("/boards", {
            method: "GET",
            query: {
              slug: params?.slug,
              ownerId: params?.ownerId,
              organizationId: params?.organizationId,
              limit: params?.limit ?? 50,
              offset: params?.offset ?? 0
            },
            headers
          });
          if (isErrorResponse(response)) {
            const errorResponse = response;
            throw toError(errorResponse.error);
          }
          return response.data ?? [];
        } catch (error) {
          throw error;
        }
      }
    }),
    detail: (boardId) => ({
      queryKey: [boardId],
      queryFn: async () => {
        if (!boardId) return null;
        try {
          const response = await client("/boards/:id", {
            method: "GET",
            params: { id: boardId },
            headers
          });
          if (isErrorResponse(response)) {
            const errorResponse = response;
            throw toError(errorResponse.error);
          }
          return response.data ?? null;
        } catch (error) {
          throw error;
        }
      }
    }),
    // Get board by slug
    bySlug: (slug) => ({
      queryKey: ["slug", slug],
      queryFn: async () => {
        if (!slug) return null;
        try {
          const response = await client("/boards", {
            method: "GET",
            query: { slug, limit: 1 },
            headers
          });
          if (isErrorResponse(response)) {
            const errorResponse = response;
            throw toError(errorResponse.error);
          }
          const boards = response.data ?? [];
          return boards[0] ?? null;
        } catch (error) {
          throw error;
        }
      }
    })
  });
}

exports.createKanbanQueryKeys = createKanbanQueryKeys;
