import { jsonToGraphQLQuery } from 'json-to-graphql-query';

import { presentTexts } from '@/app/api-utlities/presenters';
import { textsRequest } from '@/app/api-utlities/requests/common';

import { taylorAPI } from '@/shared/api/taylor-api';
import { TextItem } from '@/shared/types/text.types';

export const textControlApi = taylorAPI.injectEndpoints({
  endpoints: (builder) => ({
    fetchText: builder.query<TextItem[], void>({
      query: () => ({
        url: '',
        method: 'POST',
        body: {
          query: jsonToGraphQLQuery({ query: textsRequest }),
        },
      }),

      transformResponse: (response: any) => {
        return presentTexts(response.data._kontentSajta);
      },
    }),
  }),
});
