import { put, takeEvery, all, call } from 'redux-saga/effects';

import gql from 'graphql-tag';

// Action type
import { StoreAction } from 'store/utils/actions';

import { genericApolloSaga } from 'store/utils/apollo';

import { config, zeus } from 'config/general';

import { deepOmit } from 'store/utils/helper';
import actions from './actions';
import selectors from './selectors';
import * as models from './models';

const { Zeus, $ } = zeus;

export const treatmentOut = values => {
  const output = {
    ...values,
  };

  return deepOmit(output, ['_id', '__typename', 'createdAt', 'updatedAt', 'key', 'value']);
};

function* list(action: StoreAction) {
  action.payload = action.payload || {};
  const payload = action.payload;

  const search = payload.data || {};

  const zeusQuery = Zeus.query({
    errorSettingsGetMany: [search, models.errorModel],
  });

  payload.query = gql`
    ${zeusQuery}
  `;

  yield call(genericApolloSaga('errorSettingsGetMany', actions.list.result.action), action);
}

function* detail(action) {
  action.payload = action.payload || {};
  const payload = action.payload;

  const search = { id: payload.id };

  const zeusQuery = Zeus.query({
    errorSettingsGetOne: [search, models.errorModel],
  });

  payload.query = gql`
    ${zeusQuery}
  `;

  yield call(genericApolloSaga('errorSettingsGetOne', actions.detail.result.action), action);
}

/**
 * MUTATIONS
 */

function* add(action: StoreAction) {
  action.payload = action.payload || {};
  const payload = action.payload;

  const zeusMutation = Zeus.mutation({
    errorSettingsAddOne: [
      {
        input: $`input`,
      },
      models.errorModel,
    ],
  });

  console.log(action.payload);

  payload.mutation = gql`
    ${zeusMutation}
  `;

  payload.data = {
    input: payload.data && treatmentOut(payload.data),
  };

  yield call(genericApolloSaga('errorSettingsAddOne', actions.add.result.action), action);
}

function* update(action: StoreAction) {
  action.payload = action.payload || {};
  const payload = action.payload;

  const zeusMutation = Zeus.mutation({
    errorSettingsEditOne: [
      {
        id: payload.id,
        input: $`input`,
      },
      models.errorModel,
    ],
  });

  payload.mutation = gql`
    ${zeusMutation}
  `;

  payload.data = {
    input: payload.data && treatmentOut(payload.data),
  };

  yield call(genericApolloSaga('errorSettingsEditOne', actions.update.result.action), action);
}

function* deleteerror(action: StoreAction) {
  action.payload = action.payload || {};
  const payload = action.payload;

  const zeusMutation = Zeus.mutation({
    errorSettingsDeleteOne: [
      {
        id: payload.id,
      },
      models.errorModel,
    ],
  });

  payload.mutation = gql`
    ${zeusMutation}
  `;

  yield call(genericApolloSaga('errorSettingsDeleteOne', actions.delete.result.action), action);
}

function* errorWatchers() {
  yield all([
    takeEvery(actions.list.request.constant, list),
    takeEvery(actions.detail.request.constant, detail),
    takeEvery(actions.add.request.constant, add),
    takeEvery(actions.update.request.constant, update),
    takeEvery(actions.delete.request.constant, deleteerror),
  ]);
}

export default function* saga() {
  yield call(errorWatchers);
}
