import { GraphQLError } from 'graphql';
import { ChargesMatcherProvider } from '../providers/charges-matcher.provider.js';
import type { ChargesMatcherModule } from '../types.js';

export const autoMatchChargesResolver: ChargesMatcherModule.Resolvers = {
  Mutation: {
    autoMatchCharges: async (_, __, { injector }) => {
      try {
        const result = await injector.get(ChargesMatcherProvider).autoMatchCharges();
        return result;
      } catch (e) {
        if (e instanceof GraphQLError) {
          throw e;
        }
        const message =
          (e as Error)?.message ??
          (e as { errors: Error[] })?.errors.map(e => e.message).toString() ??
          'Unknown error';
        throw new GraphQLError(message);
      }
    },
  },
};
