import fs from 'fs'
import os from 'os'
import path from 'path'

const xmlPath = path.join(os.tmpdir(), 'gis-doc-raw.xml')
const x = fs.readFileSync(xmlPath, 'utf8')

function strip(chunk) {
  return chunk
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"')
    .replace(/&amp;/g, '&')
    .replace(/<w:tab[^>]*\/>/g, '\t')
    .replace(/<\/w:p>/g, '\n')
    .replace(/<[^>]+>/g, '')
    .replace(/\n{3,}/g, '\n\n')
}

const text = strip(x)
const keys = ['多边形', 'polygon', '周边', '2.2', '2.3', 'longitude', 'latitude', 'radius', 'page_size', 'pageSize', 'POI', '搜索', 'nearby', 'agis/pois']

for (const k of keys) {
  const i = text.indexOf(k)
  if (i >= 0) {
    console.log(`\n######## ${k} @ ${i} ########`)
    console.log(text.slice(Math.max(0, i - 150), i + 1200))
  }
}

// dump section around "2.2"
const i22 = text.indexOf('2.2')
if (i22 >= 0) {
  console.log('\n######## FULL 2.2 SECTION ########')
  console.log(text.slice(i22, i22 + 4000))
}
