import config from '@/config'
const mapKey = config.mapKey;
const appName = config.appName;

import addressData from './address'


/**
 * 腾讯位置服务地图选点
 */
const chooseLocationInit = () => {
  const key = mapKey; //使用在腾讯位置服务申请的key
  const referer = appName; //调用插件的app的名称
  wx.navigateTo({
    url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`,
  });

}

/**
 * 腾讯位置服务城市选择器
 * @param {*} city 城市名称
 */
const citySelectorNavigateTo = (city) => {
  const key = mapKey; // 使用在腾讯位置服务申请的key
  const referer = appName; // 调用插件的app的名称
  const hotCitys = '重庆'; // 用户自定义的的热门城市

  wx.navigateTo({
    url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}`,
  })
}

function splitAddress(address) {
  // 处理直辖市（如北京市朝阳区）
  const directCityRegex = /^(北京|天津|上海|重庆)(市)?(.+?区)/;
  const directMatch = address.match(directCityRegex);
  if (directMatch) {
    return {
      province: directMatch[1],
      city: directMatch[1] + '市',
      district: directMatch[3],
      detail: address.replace(directCityRegex, '')
    }
  }

  // 常规拆分逻辑 
  const provinceRegex = /(.+?(省|自治区|行政区|特别行政区))/;
  const cityRegex = /(.+?(市|自治州|州|盟))/;

  const province = address.match(provinceRegex)?.[1] || '';
  const city = address.replace(province, '').match(cityRegex)?.[1] || '';
  const remaining = address.replace(province + city, '');
  const district = remaining.match(/(.+?( 区|县|市|旗))/)?.[1] || '';
  const detail = remaining.replace(district, '').trim();

  return { province, city, district, detail };
}


function normalizeAddress(str) {
  return str.replace(/(市|区|省)/g, '').trim();
}
/**
 * 根据地址字符串返回 name 数组、code 数组、index 路径
 * @param {string} address 例如："重庆 重庆市 永川区"
 * @returns {Object|null} 包含 data/name、code、index 的对象
 */
const getCityCode = (address) => {
  // //address:重庆市永川区人民大道191号
  // const names = normalizeAddress(address).split(/\s+/);
  // let result = [];

  // // 查找省份
  // let province = addressData.find(p => p.name.includes(names[0]));
  // if (!province) return [];

  // result.push(province.code);

  // if (names.length < 2) return result;

  // // 查找城市
  // let city = province.children?.find(c => c.name.includes(names[1]));
  // if (!city) return result;

  // result.push(city.code);

  // if (names.length < 3) return result;

  // // 查找区县
  // let district = city.children?.find(d => d.name.includes(names[2]));
  // if (!district) return result;

  // result.push(district.code);

  // return result;

  const names = address.split(/\s+/)
  const result = {
    data: [],
    code: [],
    index: []
  }

  // 查找省份
  const provinceIndex = addressData.findIndex(p => p.name.includes(names[0]))
  if (provinceIndex === -1) return null

  const province = addressData[provinceIndex]
  result.data.push(province.name)
  result.code.push(province.code)
  result.index.push(provinceIndex)

  if (names.length < 2) return result

  // 查找城市
  const cityIndex = province.children.findIndex(c => c.name.includes(names[1]))
  if (cityIndex === -1) return result

  const city = province.children[cityIndex]
  result.data.push(city.name)
  result.code.push(city.code)
  result.index.push(cityIndex)

  if (names.length < 3) return result

  // 查找区县
  const districtIndex = city.children.findIndex(d => d.name.includes(names[2]))
  if (districtIndex === -1) return result

  const district = city.children[districtIndex]
  result.data.push(district.name)
  result.code.push(district.code)
  result.index.push(districtIndex)

  return result


}
export {
  chooseLocationInit,
  citySelectorNavigateTo,
  splitAddress,
  getCityCode
}
