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

import { presentMedia } from '@/app/api-utlities/presenters';
import { mediaRequest } from '@/app/api-utlities/requests/common';

import { taylorAPI } from '@/shared/api/taylor-api';
import { MediaItem } from '@/shared/types/media.type';

export const mediaControlApi = taylorAPI.injectEndpoints({
  endpoints: (builder) => ({
    fetchMedia: builder.query<MediaItem[], void>({
      query: () => ({
        url: '',
        method: 'POST',
        body: {
          query: jsonToGraphQLQuery({ query: mediaRequest }),
        },
      }),
      transformResponse: (response: any) => {
        return presentMedia(response.data.mediaKontentSajta);
      },
    }),
  }),
});
