import { regionData } from "element-china-area-data"

export { regionData }

/** 根据级联 code 数组解析省/市/县名称 */
export function resolveRegionLabels(codes) {
  const labels = []
  if (!codes || !codes.length) {
    return labels
  }
  let nodes = regionData
  for (const code of codes) {
    const node = nodes.find((n) => n.value === code)
    if (!node) {
      break
    }
    labels.push(node.label)
    nodes = node.children || []
  }
  return labels
}

/** 从牧场行数据还原级联选中值 */
export function pastureToRegionCodes(row) {
  if (!row || !row.provinceCode) {
    return []
  }
  const codes = [row.provinceCode, row.cityCode, row.countyCode].filter((c) => c != null && c !== "")
  return codes.length === 3 ? codes : []
}
