'use strict'
!(function(require, directRequire) {
  const a = require('react'),
    { connect: b } = require('react-redux'),
    c = require('./579ff317e2bba616b0afc89a505c4f3e.js'),
    d = require('./db2217eb4cff896bdcbc50abe005058f.js'),
    e = require('./6ff091369f442a4678a2ed4a1f758495.js'),
    f = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    g = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    h = require('./ff946754202ecf377034d29daac7c8d9.js'),
    i = require('./a1dd553cc059d528bb0ef56afed53968.js'),
    j = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    k = require('./2dfc6a3df6d6fc51266b293c8420e88b.js'),
    l = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    m = a => Object.prototype.toString.call(a).slice(8, -1)
  class n extends a.Component {
    constructor(a) {
      super(a), (this.state = { open_ticket: '' })
    }
    componentDidMount() {
      this.getTicket(), this.initGetWindow()
    }
    componentWillUnmount() {}
    async getTicket() {
      try {
        const { body: a } = await g({
          url: `${f.getDataOpenTicket}?appid=${this.props.appid}`,
          needToken: 1
        })
        console.log('res', a), this.setState({ open_ticket: a.open_ticket })
      } catch (a) {
        console.info(`custom-analysis init error: ${a}`),
          this.props.closeCustomAnalysis()
      }
    }
    async initGetWindow() {
      this.win = await d.onWindowRegistered('custom_analysis')
      const a = this.win.window.navigator.userAgent
      Object.defineProperty(this.win.window.navigator, 'userAgent', {
        get: () => `${a} devtools`
      }),
        console.log('init popup done')
    }
    onMessage(a) {
      if ('Object' === m(a.data) && 'GET_APP_CONFIG' === a.data.type) {
        const b = JSON.stringify(a.data.config || [])
        this.props.updateCustomAnalysisConfig(b),
          i.triggerOnEvent({
            eventName: 'onAppConfig',
            data: { data: a.data.config, type: 1 }
          }),
          k.display({
            command: l.DISPLAY_INFO,
            type: l.DISPLAY_TYPES.CUSTOM_ANALYSIS_ON_APP_CONFIG,
            data: { config: b }
          })
      }
    }
    onWindowOpen(a) {
      ;(this.win = a),
        a.on('new-win-policy', function(a, b, c) {
          c.ignore(), nw.Shell.openExternal(b)
        })
    }
    onWindowClose() {
      this.props.closeCustomAnalysis()
    }
    render() {
      if (!this.state.open_ticket) return null
      let b, d
      try {
        ;(b = 1200), (d = nw.Screen.screens[0].work_area.height)
        for (const a of nw.Screen.screens) {
          const {
            work_area: { width: c, height: e }
          } = a
          b > c && (b = c), d > e && (d = e)
        }
      } catch (a) {
        ;(b = 1200),
          (d = this.props.win ? this.props.win.height : global.Win.height)
      }
      return a.createElement(c, {
        width: b,
        height: d,
        registryId: 'custom_analysis',
        url: `https://mp.weixin.qq.com/wxopen/userdefineanalysis_ide?action=event_list&dev_code=${
          this.state.open_ticket
        }&appid=${this.props.appid}&devtools=1`,
        onMessage: this.onMessage.bind(this),
        onWindowOpen: this.onWindowOpen.bind(this),
        onWindowClose: this.onWindowClose.bind(this)
      })
    }
  }
  module.exports = b(
    a => {
      return { appid: a.project.current.appid }
    },
    a => {
      return {
        closeCustomAnalysis: () => a(e.closeCustomAnalysis()),
        updateCustomAnalysisConfig: b => a(e.updateCustomAnalysisConfig(b))
      }
    }
  )(n)
})(require('lazyload'), require)
