/* eslint-disable no-shadow */
import http from '../../utils/http';
/**
 * 获取外部跳转商品店铺.
 *
 * @param param0
 * @returns
 */

function getJumpStoreId({
  productId,
  longitude,
  latitude
}) {
  return http.get({
    url: '/shop/getDetailByProductId',
    data: {
      productId,
      longitude: longitude || null,
      latitude: latitude || null
    }
  }).then(res => {
    return res;
  });
}
/**
 * 获取店铺详情.
 *
 * @param param0
 * @returns
 */


function getDetail({
  storeId,
  cityCode,
  coordinate,
  shopType
}) {
  return http.get({
    url: '/shop/getDetail',
    data: {
      shopType,
      storeId,
      cityCode,
      coordinate: coordinate ? `${coordinate.longitude};${coordinate.latitude}` : undefined
    }
  }).then(({
    distance,
    coordinate,
    ...rest
  }) => {
    distance = formatDistance(distance); // 经纬度 分号分割，经度在前纬度在后

    const [longitude, latitude] = coordinate.split(';');
    const store = { ...rest,
      distance,
      latitude,
      longitude
    };
    return store;
  });
}
/**
 * 获取附近店铺.
 *
 * @param param0
 * @returns
 */


function getNearStore({
  coordinate,
  shopType,
  ...rest
}) {
  return http.get({
    url: '/shop/getNearStore',
    loading: false,
    data: { ...rest,
      shopType,
      coordinate: coordinate ? `${coordinate.longitude};${coordinate.latitude}` : undefined
    }
  });
}
/**
 * 获取店铺列表筛选条件.
 *
 * @param cityCode 城市编码.
 * @returns
 */


function getSearchList(cityCode) {
  return http.get({
    url: '/shop/getSearchList',
    data: {
      cityCode
    }
  });
}
/**
 * 店铺列表搜索.
 *
 * @param param0
 * @returns
 */


function getStoreList({
  coordinate,
  center,
  labelsFilter,
  ...rest
}) {
  return http.post({
    url: '/shop/getStoreList',
    loading: false,
    data: {
      coordinate: coordinate ? `${coordinate.longitude};${coordinate.latitude}` : undefined,
      targetLocation: center ? `${center.longitude};${center.latitude}` : undefined,
      ...rest
    }
  }).then(({
    data,
    ...rest
  }) => ({ ...rest,
    data: data.map(({
      distance,
      coordinate,
      labels,
      ...storeRest
    }) => {
      distance = formatDistance(distance); // 经纬度 分号分割，经度在前纬度在后

      const [longitude, latitude] = coordinate.split(';'); // 标签数据无id

      labels = labels.map((lbl, idx) => {
        lbl.id = lbl.id || idx.toString();
        return lbl;
      }); // 标签个数处理，最多显示固定个数

      if (labels && !labelsFilter) {
        if (storeRest.logo && labels.length > 3) {
          labels = labels.slice(0, 3);
        }

        if (!storeRest.logo && labels.length > 5) {
          labels = labels.slice(0, 5);
        }
      }

      if (labels && labelsFilter) {
        labels = labelsFilter(labels, storeRest.logo);
      }

      const store = { ...storeRest,
        distance,
        labels,
        latitude,
        longitude
      };
      return store;
    })
  }));
}

function getStoreStyle() {
  return http.get({
    url: '/shop/getStyle',
    errmsg: false,
    loading: false
  });
}
/**
 * 查询地区poi信息.
 *
 * @param search
 */


function getAddressPoi(search) {
  return http.get({
    url: '/map/place/text',
    data: {
      search
    }
  }).then(place => {
    const poi = {};

    if (place.adcode) {
      const code = place.adcode.substring(0, 4);
      poi.cityCode = `${code}00`;
    }

    if (place.location) {
      const [longitude, latitude] = place.location.split(',');
      poi.coordinate = {
        longitude,
        latitude
      };
    }

    return poi;
  });
}
/**
 * 查询位置信息.
 *
 * @param search
 */


function getLocationInfo(search) {
  return http.get({
    url: '/map/place/text',
    data: {
      search
    }
  }).then(place => {
    const locationInfo = {
      province: place.pname,
      city: place.cityname,
      cityCode: place.adcode,
      address: place.address,
      latitude: place.location,
      longitude: place.location
    };

    if (locationInfo.cityCode) {
      const code = locationInfo.cityCode.substring(0, 4);
      locationInfo.cityCode = `${code}00`;
    }

    if (place.location) {
      const [longitude, latitude] = place.location.split(',');
      locationInfo.latitude = latitude;
      locationInfo.longitude = longitude;
    }

    return locationInfo;
  });
}
/**
 * 附近城市列表.
 *
 * @param storeId 店铺id.
 * @returns
 */


function getCityList(storeId) {
  return http.get({
    url: '/shop/getCityList',
    data: {
      storeId
    }
  });
}

export { getDetail, getNearStore, getSearchList, getStoreList, getStoreStyle, getAddressPoi, getLocationInfo, getCityList, getJumpStoreId };
/**
 * 格式化距离.
 *
 * @param distance 距离，单位M.
 * @returns
 */

function formatDistance(distance) {
  const num = Number(distance);

  if (num) {
    if (num < 1000) {
      distance = `${Math.ceil(num).toString()}m`;
    } else {
      distance = `${Math.ceil(num / 1000).toString()}km`;
    }
  }

  return distance;
}