/* eslint-disable no-unused-vars */

/* eslint-disable arrow-parens */
import request from '../../service/request';
import { urlConfig, saasConfig } from '../../utils/mock';
/*
 * 下单
 * @param authCode
 * */

const createOrder = params => {
  const url = `${urlConfig}/api/v1/mall/order/create`;
  const {
    formId: syncOrderAuthCode,
    ...rest
  } = params;
  return request.post(url, { ...rest,
    syncOrderAuthCode
  });
};
/*
 * 查询是否有手机号权限
 * */


const queryForPhonePermission = () => {
  const url = `${saasConfig}/api/v1/appUserInfo/queryApiPermission`;
  return request.get(url, {
    applyApi: 'MOBILE'
  });
};
/*
 * 查询地址距离
 * */


const queryForAddressDistance = params => {
  const url = `${urlConfig}/api/v1/mall/address/distance`;
  return request.get(url, params); // return new Promise(v => {
  //   v({
  //     distance: '3km',
  //     flag: true,
  //   });
  // });
};
/*
 * 查询收货地址或基础信息
 * */


const queryForAddressList = () => {
  const url = `${urlConfig}/api/v1/mall/address/list`;
  return request.get(url, {
    current: 1,
    pageSize: 999,
    status: true
  });
};
/*
 * 查询收货地址或基础信息
 * */


const queryForLastAddressList = () => {
  const url = `${urlConfig}/api/v1/mall/address/getLastUserOrderAddress`;
  return request.get(url);
};
/*
 * 查询手机号
 * @param 无参数
 * */


const getPhoneNumber = (params = '') => new Promise(async resolve => {
  const url = `${urlConfig}/api/v1/mall/consignee/fromLatestOrder`;

  try {
    const {
      phone = '',
      phoneFromOrderId = ''
    } = (await request.post(url, params)) || {};
    resolve({
      phone,
      phoneFromOrderId,
      isPhoneReady: true
    });
  } catch (err) {
    console.log(err);
    resolve({
      phone: '',
      phoneFromOrderId: '',
      isPhoneReady: true
    });
  }
});

export default {
  createOrder,
  getPhoneNumber,
  queryForLastAddressList,
  queryForAddressList,
  queryForPhonePermission,
  queryForAddressDistance
};