//ANRAN_REGION_DATA from cdn/jsdata
// 深拷贝数组
// let scriptEle = document.createElement('script')
// scriptEle.setAttribute('src', 'http://upload.ar.wqcms.com/cdn/jsdata/ar_region_data.js?ver=0.1')
// document.getElementsByTagName('head')[0].appendChild(scriptEle)
// // document.write("<script language='javascript' src='http://upload.ar.wqcms.com/cdn/jsdata/ar_region_data.js?ver=0.1'></script>");
// require('http://tool.anran.io/cdn/jsdata/ar_region_data.js')
import axios from 'axios'
import store from '@/utils/vuexStore'
import {REGION_URL} from '@/utils/config'
import importScript from './importScript'

let regionScriptLoaded = false

const initRegion = function (vueObj) {

  importScript(REGION_URL, () => {
    if (regionScriptLoaded) return true
    regionScriptLoaded = true
    // let dataResult = response.data.replace(/^const\s+AR_REGION_DATA\s+\=\s+/, '')
    // dataResult = dataResult.replace(/\;$/, '')
    // //dataResult = JSON.parse(dataResult)
    // dataResult = eval('('+dataResult+')')
    // let ANRAN_REGION_DATA = dataResult
    let ANRAN_REGION_DATA = AR_REGION_DATA

    // code转汉字大对象
// 汉字转code大对象
    const rootCode = '86'
    vueObj.$set(store.state.regionInfo.CodeToText, '', '全部')

// 计算省
    for (const prop in ANRAN_REGION_DATA[rootCode]) {
      store.state.regionInfo.regionData.push({
        value: prop,
        label: ANRAN_REGION_DATA[rootCode][prop]
      })
      store.state.regionInfo.CodeToText[prop] = ANRAN_REGION_DATA[rootCode][prop]
      store.state.regionInfo.TextToCode[ANRAN_REGION_DATA[rootCode][prop]] = {
        code: prop
      }
      store.state.regionInfo.TextToCode[ANRAN_REGION_DATA[rootCode][prop]]['全部'] = {
        code: ''
      }
    }

// 计算市
    for (let i = 0, len = store.state.regionInfo.regionData.length; i < len; i++) {
      const provinceCode = store.state.regionInfo.regionData[i].value
      const provinceText = store.state.regionInfo.regionData[i].label
      const provinceChildren = []
      for (const prop in ANRAN_REGION_DATA[provinceCode]) {
        provinceChildren.push({
          value: prop,
          label: ANRAN_REGION_DATA[provinceCode][prop]
        })
        store.state.regionInfo.CodeToText[prop] = ANRAN_REGION_DATA[provinceCode][prop]
        store.state.regionInfo.TextToCode[provinceText][ANRAN_REGION_DATA[provinceCode][prop]] = {
          code: prop
        }
        store.state.regionInfo.TextToCode[provinceText][ANRAN_REGION_DATA[provinceCode][prop]]['全部'] = {
          code: ''
        }
      }
      if (provinceChildren.length) {
        store.state.regionInfo.regionData[i].children = provinceChildren
      }
    }

    store.state.regionInfo.provinceAndCityData = cloneArray(store.state.regionInfo.regionData)

// 计算区
    for (let i = 0, len = store.state.regionInfo.regionData.length; i < len; i++) {
      const province = store.state.regionInfo.regionData[i].children
      const provinceText = store.state.regionInfo.regionData[i].label
      if (province) {
        for (let j = 0, len = province.length; j < len; j++) {
          const cityCode = province[j].value
          const cityText = province[j].label
          const cityChildren = []
          for (const prop in ANRAN_REGION_DATA[cityCode]) {
            cityChildren.push({
              value: prop,
              label: ANRAN_REGION_DATA[cityCode][prop]
            })
            store.state.regionInfo.CodeToText[prop] = ANRAN_REGION_DATA[cityCode][prop]
            store.state.regionInfo.TextToCode[provinceText][cityText][ANRAN_REGION_DATA[cityCode][prop]] = {
              code: prop
            }
          }
          if (cityChildren.length) {
            province[j].children = cityChildren
          }
        }
      }
    }

// 添加“全部”选项
    store.state.regionInfo.provinceAndCityDataPlus = cloneArray(store.state.regionInfo.provinceAndCityData)
    store.state.regionInfo.provinceAndCityDataPlus.unshift({
      value: '',
      label: '全部'
    })
    /*for (let i = 0, len = store.state.regionInfo.provinceAndCityDataPlus.length; i < len; i++) {
      const province = store.state.regionInfo.provinceAndCityDataPlus[i].children
      if (province && province.length) {
        province.unshift({
          value: '',
          label: store.state.regionInfo.provinceAndCityDataPlus[i].label + '全部'
        })

        for (let j = 0, len = province.length; j < len; j++) {
          const city = province[j].children
          if (city && city.length) {
            city.unshift({
              value: '',
              label: province[j].label + '全部'
            })
          }
        }
      }
    }*/

    store.state.regionInfo.regionDataPlus = cloneArray(store.state.regionInfo.regionData)
    store.state.regionInfo.regionDataPlus.unshift({
      value: '',
      label: '全部'
    })
    /*for (let i = 0, len = store.state.regionInfo.regionDataPlus.length; i < len; i++) {
      const province = store.state.regionInfo.regionDataPlus[i].children
      if (province && province.length) {
        province.unshift({
          value: '',
          label: store.state.regionInfo.regionDataPlus[i].label + '全部'
        })

        for (let j = 0, len = province.length; j < len; j++) {
          const city = province[j].children
          if (city && city.length) {
            city.unshift({
              value: '',
              label: province[j].label + '全部'
            })
          }
        }
      }
    }*/
  })
}


//let ANRAN_REGION_DATA = AR_REGION_DATA
const cloneArray = function (obj) {
  const newArray = []
  for (const i in obj) {
    newArray[i] = typeof obj[i] === 'object' ? cloneArray(obj[i]) : obj[i]
  }
  return newArray
}

export default initRegion
