import Store from 'herculex';
import request from '../../utils/request';
import { getAccessMode } from '../../utils';
import commonState from '../one/store/state';
import mutations from '../one/store/mutations';
import commonActions from '../one/store/actions';
import { getIn } from 'herculex/dist/utils/manipulate';
import { getServiceInfoUrl } from '../../utils/constants';


const initalState = JSON.parse(JSON.stringify(commonState));

export default new Store({
  connectGlobal: true,
  state: {
    ...initalState,
    isLoading: true,
    isShowModal: false,
    isExitApp: false,
    url: '',
    serviceUUID: '',
    serviceCode: '',
    errorType: '',
    errorMsg: '',
    service: {},
    resParams: {},
  },
  getters: {
  },
  mutations: {
    ...mutations,
  },
  actions: {
    ...commonActions,
    async init({ dispatch, dispatchGlobal, state, commit }) {
      await dispatchGlobal('initialize');
      const {
        memberCardComponent,
        accessMode: _accessMode,
      } = await dispatch('getService');
      commit({ isLoading: false });
      const { customer } = getAccessMode(_accessMode);
      const payload = { ...state.service, from: 'webview' };
      if (customer) {
        payload.customerCheckInPage = () => (
          dispatch('customerCheck', { memberCardComponent })
        );
      }
      const jumpRes = await dispatchGlobal('handleJumpService', payload);
      my.hideLoading();
      if (jumpRes && jumpRes.url) {
        const { resParams } = state;
        const url = Object.keys(resParams).reduce((acc, value) => `${acc}&${value}=${resParams[value]}`, jumpRes.url);
        commit({
          isShowModal: false,
          url,
        });
      }
    },
    async webViewShowService({ dispatch, state, commit }) {
      const payload = { ...state.service, from: 'webview' };
      if (state.accessMode === 'customer') {
        payload.customerCheckInPage = dispatch.bind(this, 'customerCheck', { memberCardComponent: getIn(state, ['service', 'memberCardComponent'], {}) });
      }
      const jumpRes = await this.dispatchGlobal('handleJumpService', payload);
      my.hideLoading();
      if (jumpRes && jumpRes.url) {
        const { resParams } = state;
        const url = Object.keys(resParams).reduce((acc, value) => `${acc}&${value}=${resParams[value]}`, jumpRes.url);
        commit({
          isShowModal: false,
          url,
        });
      }
    },

    // 获取服务信息
    async getService({ commit, state }) {
      try {
        const [err, res] = await request({
          url: getServiceInfoUrl,
          data: {
            serviceUUID: state.serviceUUID,
            serviceCode: state.serviceCode,
          },
        });
        if (err || !res) throw err;
        commit({
          service: {
            ...res,
            from: 'webview',
          },
        });
        return { accessMode: res.accessMode, memberCardComponent: res.memberCardComponent };
      } catch (error) {
        commit({
          errorType: 1,
        });
      }
    },
  },
});
