import UPDATE_CONTENT_BOOKMARK from '@/graphql/gql/mutations/updateContentBookmark.gql';
import UPDATE_INSTRUMENT_BOOKMARK from '@/graphql/gql/mutations/updateInstrumentBookmark.gql';
import CONTENT_BLOCK_QUERY from '@/graphql/gql/queries/contentBlockQuery.gql';
import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql';
import { pushToArray, removeAtIndex } from '@/graphql/immutable-operations';

const compareUuid = (uuid) => (element) => element.uuid === uuid;

export const constructContentComponentBookmarkMutation = (uuid, bookmarked, parent, root) => {
  let mutation = {};

  if (parent.__typename === 'InstrumentNode') {
    mutation = {
      mutation: UPDATE_INSTRUMENT_BOOKMARK,
      variables: {
        input: {
          uuid,
          instrument: root,
          bookmarked,
        },
      },
      update: (store) => {
        const fragment = INSTRUMENT_FRAGMENT;
        const id = store.identify({
          __typename: 'InstrumentNode',
          slug: root,
        });
        const instrument = store.readFragment({
          fragment,
          id,
        });

        let bookmarks;

        if (bookmarked) {
          const element = {
            note: null,
            uuid,
            __typename: 'InstrumentBookmarkNode',
          };
          bookmarks = pushToArray(instrument.bookmarks, element);
        } else {
          let index = instrument.bookmarks.findIndex(compareUuid(uuid));
          if (index > -1) {
            bookmarks = removeAtIndex(instrument.bookmarks, index);
          } else {
            bookmarks = [...instrument.bookmarks];
          }
        }

        const data = {
          ...instrument,
          bookmarks,
        };

        store.writeFragment({
          data,
          fragment,
          id,
        });
      },
      optimisticResponse: {
        __typename: 'Mutation',
        updateInstrumentBookmark: {
          __typename: 'UpdateInstrumentBookmarkPayload',
          success: true,
        },
      },
    };
  } else {
    mutation = {
      mutation: UPDATE_CONTENT_BOOKMARK,
      variables: {
        input: {
          uuid,
          contentBlock: root,
          bookmarked,
        },
      },
      update: (store) => {
        const query = CONTENT_BLOCK_QUERY;
        const variables = { id: root };
        const { contentBlock } = store.readQuery({
          query,
          variables,
        });

        // const bookmarks = data.contentBlock.bookmarks;
        let bookmarks;

        if (bookmarked) {
          let element = {
            note: null,
            uuid,
            __typename: 'ContentBlockBookmarkNode',
          };
          bookmarks = pushToArray(contentBlock.bookmarks, element);
        } else {
          let index = contentBlock.bookmarks.findIndex(compareUuid(uuid));
          if (index > -1) {
            bookmarks = removeAtIndex(contentBlock.bookmarks, index);
          } else {
            bookmarks = [...contentBlock.bookmarks];
          }
        }

        const data = {
          contentBlock: {
            ...contentBlock,
            bookmarks,
          },
        };

        store.writeQuery({
          data,
          query,
          variables,
        });
      },
      optimisticResponse: {
        __typename: 'Mutation',
        updateContentBookmark: {
          __typename: 'UpdateContentBookmarkPayload',
          success: true,
        },
      },
    };
  }
  return mutation;
};
