import { mergeQueryKeys, createQueryKeys } from '@lukemorales/query-key-factory';

function isErrorResponse(response) {
  if (typeof response !== "object" || response === null) {
    return false;
  }
  const obj = response;
  return "error" in obj && obj.error !== null && obj.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 createCMSQueryKeys(client, headers) {
  const contentTypes = createContentTypesQueries(client, headers);
  const content = createContentQueries(client, headers);
  return mergeQueryKeys(contentTypes, content);
}
function createContentTypesQueries(client, headers) {
  return createQueryKeys("cmsTypes", {
    list: () => ({
      queryKey: ["list"],
      queryFn: async () => {
        try {
          const response = await client("/content-types", {
            method: "GET",
            headers
          });
          if (isErrorResponse(response)) {
            throw toError(response.error);
          }
          return response.data ?? [];
        } catch (error) {
          throw error;
        }
      }
    }),
    detail: (slug) => ({
      queryKey: [slug],
      queryFn: async () => {
        if (!slug) return null;
        try {
          const response = await client("/content-types/:slug", {
            method: "GET",
            params: { slug },
            headers
          });
          if (isErrorResponse(response)) {
            throw toError(response.error);
          }
          return response.data;
        } catch (error) {
          throw error;
        }
      }
    })
  });
}
function createContentQueries(client, headers) {
  return createQueryKeys("cmsContent", {
    list: (params) => ({
      queryKey: [params],
      queryFn: async () => {
        try {
          const response = await client("/content/:typeSlug", {
            method: "GET",
            params: { typeSlug: params.typeSlug },
            query: {
              limit: params.limit ?? 20,
              offset: params.offset ?? 0
            },
            headers
          });
          if (isErrorResponse(response)) {
            throw toError(response.error);
          }
          return response.data;
        } catch (error) {
          throw error;
        }
      }
    }),
    detail: (typeSlug, id) => ({
      queryKey: [typeSlug, id],
      queryFn: async () => {
        if (!typeSlug || !id) return null;
        try {
          const response = await client("/content/:typeSlug/:id", {
            method: "GET",
            params: { typeSlug, id },
            headers
          });
          if (isErrorResponse(response)) {
            throw toError(response.error);
          }
          return response.data;
        } catch (error) {
          throw error;
        }
      }
    }),
    bySlug: (typeSlug, slug) => ({
      queryKey: ["bySlug", typeSlug, slug],
      queryFn: async () => {
        if (!typeSlug || !slug) return null;
        try {
          const response = await client("/content/:typeSlug", {
            method: "GET",
            params: { typeSlug },
            query: { slug, limit: 1 },
            headers
          });
          if (isErrorResponse(response)) {
            throw toError(response.error);
          }
          const data = response.data;
          return data.items[0] ?? null;
        } catch (error) {
          throw error;
        }
      }
    })
  });
}

export { createCMSQueryKeys };
