import utils from '@src/common/util.js'

const UEDITOR_HOME_URL = './lib/neditor/'

async function run() {
  if (typeof window.UE === 'undefined') {
    try {
      await utils.loadJS(UEDITOR_HOME_URL + 'neditor.config.js')
    } catch (e) {
      console.error('neditor.config.js is missing', e.toString())
      return
    }
    try {
      await utils.loadJS(UEDITOR_HOME_URL + 'neditor.all.js')
      await utils.loadJS(UEDITOR_HOME_URL + 'com.zving/vendors.js')
      utils.loadJS(UEDITOR_HOME_URL + 'com.zving/mobilecontenteditor/less-1.7.0.min.js')
    } catch (e) {
      console.error('neditor.all.js is missing', e.toString())
      return
    }

    setTimeout(() => {
      loadPlugins()
    }, 600)
  } else {
    setTimeout(() => {
      loadPlugins()
    }, 600)
  }
}
async function loadPlugins() {
  await Promise.all(
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zimage.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zcatalog.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zcontent.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zresourceurl.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zaudio.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zvideo.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zfile.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zcomment.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/articlelogo.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zarticlesetting.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zuploadlocalimage.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zpagebreak.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zweiboshow.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zwritingmode.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zfontface.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/autoformat.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zimagegroup.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zcheckwords.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zimageplayer.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zatverdise.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zvote.js'),
    utils.loadJS(UEDITOR_HOME_URL + 'com.zving/zimgcutting.js')
  )
}
run()

export default run
