import { ActionContext, ActionTree, mapActions } from 'vuex';
import get from 'lodash/get';
import axios from 'axios';

const pending: Record<string, Record<string, Promise<any>>> = {
  fetchModelByCode: {},
};

export const actions: ActionTree<any, any> = {
  fetchModelByCode({ commit }: ActionContext<any, any>, code) {
    return (
      pending.fetchModelByCode[code] ||
      (pending.fetchModelByCode[code] = new Promise((resolve, reject) => {
        axios
          .get(
            `${
              process.env.VUE_APP_PROJECT_PATH || ''
            }/service/model-registry/${code}`
          )
          .then((response) => {
            const data =
              get(
                {
                  response,
                },
                'response.data'
              ) || '';

            return resolve(data);
          })
          .catch((e) => reject(e))
          .finally(() => {
            delete pending.fetchModelByCode[code];
          });
      }))
    );
  },
  // todo remove from store
  fetchAttractionsByCode({ commit }: any = {}, code) {
    return new Promise((resolve, reject) => {
      axios
        .get(
          `${
            process.env.VUE_APP_PROJECT_PATH || ''
          }/service/attraction-registry/${code}`
        )
        .then((response) => {
          const data = get({ response }, 'response.data') || '';
          return resolve(data);
        })
        .catch((e) => reject(e));
    });
  },
  // todo del - duplicates 'fetchAttractionsByCode'
  fetchGeneratedFieldsByCode({ commit }: any = {}, code) {
    return new Promise((resolve, reject) => {
      axios
        .get(
          `${
            process.env.VUE_APP_PROJECT_PATH || ''
          }/service/attraction-registry/generators_${code}`
        )
        .then((response) => {
          const data = get({ response }, 'response.data') || '';
          return resolve(data);
        })
        .catch((e) => reject(e));
    });
  },
  // todo remove from store
  prepareStatementPrintTemplate(
    { commit }: any = {},
    { schema, authorSchema, model, templates, footers }
  ) {
    return new Promise((resolve, reject) => {
      axios
        .post(
          `${
            process.env.VUE_APP_PROJECT_PATH || ''
          }/service/prepare-statement-print-template`,
          {
            schema,
            authorSchema,
            model,
            templates,
            footers
          }
        )
        .then((response) => {
          const data = get({ response }, 'response.data') || '';
          return resolve(data);
        })
        .catch((e) => reject(e));
    });
  },
  // todo remove from store
  fetchMockDataForSchema({ commit }: any = {}, schema) {
    return new Promise((resolve, reject) => {
      axios
        .post(`${process.env.VUE_APP_PROJECT_PATH || ''}/service/mock-schema`, {
          schema,
        })
        .then((response) => {
          const data = get({ response }, 'response.data') || '';
          return resolve(data);
        })
        .catch((e) => reject(e));
    });
  },
};

export const utilsComponentsMethods = mapActions('utils/components', [
  'fetchComponentByURL',
  'parseComponentByString',
  'fetchModelByCode',
]);
