'use strict'
!(function(require, directRequire) {
  const a = require('path'),
    b = require('react'),
    c = require('./a1dd553cc059d528bb0ef56afed53968.js'),
    d = require('./d62fc37d7aa6416d5dcc240ba94175cd.js'),
    e = require('./1dea83a77e99a7c94f6b6f01f5c175b0.js'),
    f = require('./dd93307b3045531926d5a6645f6f3455.js'),
    g = require('./51a8f674caffc4c2fa2358314af90837.js'),
    h = require('./logger.js'),
    i = require('./99fff845d6c7bff564f99e38e435f827.js'),
    j = require('./6620a0cf7dad1b400d60f0fdae40f524.js')
  class k extends b.Component {
    constructor(a) {
      super(a), (this.syncDialogMap = {}), (this.readyForLaunch = !1)
    }
    componentDidMount() {
      ;(this._onAppServiceMessage = this.onAppServiceMessage.bind(this)),
        c.register(this._onAppServiceMessage),
        (this._onAppDataMessage = this.onAppDataMessage.bind(this)),
        f.register(this._onAppDataMessage),
        (this._onStorageMessage = this.onStorageMessage.bind(this)),
        g.register(this._onStorageMessage),
        this.initProjectFileWatcher(),
        this.initWebview()
    }
    componentDidUpdate() {
      this.props.show &&
        !this.appended &&
        this.devtoolsview &&
        this.showDevTools()
    }
    componentWillUnmount() {
      this.devtoolsview && this.devtoolsview.remove(),
        this.webview && this.webview.remove(),
        c.unRegister(this._onAppServiceMessage),
        f.unRegister(this._onAppDataMessage),
        g.unRegister(this._onStorageMessage)
    }
    componentWillReceiveProps(a) {
      if (a.assdkCallbackInfo != this.props.assdkCallbackInfo) {
        let { callbackID: b, res: d } = a.assdkCallbackInfo
        this.syncDialogMap[b]
          ? (this.syncDialogMap[b].ok(JSON.stringify(d)),
            delete this.syncDialogMap[b])
          : c.invokeCallback(b, d)
      }
      a.appLaunched != this.props.appLaunched &&
        !1 === a.appLaunched &&
        this.restart(),
        a.project != this.props.project && this.initProjectFileWatcher(),
        this.readyForLaunch &&
          !this.appLaunched &&
          a.currentWebview &&
          a.currentWebview.ready &&
          setTimeout(() => {
            this.appLaunch()
          }),
        a.show != this.props.show && a.show && (this.appended = !1)
    }
    initWebview() {
      this.devtoolsview && this.devtoolsview.remove(),
        this.webview && this.webview.remove()
      let a = (this.devtoolsview = document.createElement('webview')),
        b = (this.webview = document.createElement('webview'))
      b.setUserAgentOverride(`appservice port/${global.messageCenterPort}`),
        b.setAttribute('partition', 'persist:appservice'),
        b.setAttribute('tabIndex', '-1'),
        this.onSyncMessage(b)
      let c = `${a.getUserAgent()} appservicedevtools port/${
        global.messageCenterPort
      } proxy/${global.proxyPort}`
      a.setUserAgentOverride(c),
        a.setAttribute('partition', 'persist:devtools'),
        a.setAttribute('style', 'height:100%;width:100%;position:absolute;'),
        this.refs.container.appendChild(b),
        this.restart(),
        this.props.show && this.showDevTools()
    }
    showDevTools() {
      let a = this.devtoolsview
      this.refs.popupcontainer.appendChild(a)
      const b = () => {
        a.removeEventListener('loadcommit', b),
          this.webview.showDevTools(!0, this.devtoolsview)
      }
      a.addEventListener('loadcommit', b),
        (a.src = 'about:blank'),
        (this.appended = !0)
    }
    appLaunch() {
      let a = this.props
      this.readyForLaunch &&
        !this.appLaunched &&
        a.currentWebview &&
        a.currentWebview.ready &&
        (c.triggerOnEvent({
          eventName: 'onAppRoute',
          data: {
            path: a.currentWebview.pathName + '.html',
            query: a.currentWebview.query,
            openType: 'appLaunch'
          }
        }),
        this.props.simulatorActions.setAppLaunched(!0),
        (this.appLaunched = !0))
    }
    restart() {
      if (this.loading) return
      this.props.simulatorActions.setAppLaunched(!1)
      let a = () => {
          this.webview.removeEventListener('loadstop', a),
            (this.readyForLaunch = !0),
            (this.loading = !1),
            this.appLaunch()
        },
        b = `http://${
          this.props.project.hash
        }.appservice.open.weixin.qq.com/appservice`
      ;(this.readyForLaunch = !1),
        (this.loading = !0),
        (this.appLaunched = !1),
        (this.webview.src = b),
        this.webview.addEventListener('loadstop', a)
    }
    onAppServiceMessage(a) {
      let { command: b, data: d } = a
      'APPSERVICE_PUBLISH' == b
        ? c.publish(a)
        : 'APPSERVICE_INVOKE' == b
          ? this.props.assdkActions.exec(d)
          : 'SEND_APP_DATA' == b &&
            f.send({ command: 'SEND_APP_DATA', data: d })
    }
    onAppDataMessage(a) {
      let { command: b, data: d } = a
      'GET_APP_DATA' == b
        ? f.send({ command: 'GET_APP_DATA' })
        : 'WRITE_APP_DATA' == b &&
          c.send({ command: 'WRITE_APP_DATA', data: d })
    }
    onStorageMessage(a) {
      let { command: b, data: c } = a
      'EXEC_STORAGE_SDK' == b &&
        (console.error(c), this.props.assdkActions.exec(c))
    }
    onSyncMessage(a) {
      a.addEventListener('dialog', a => {
        let b = a.messageType || '',
          c = a.messageText
        if ('prompt' === b && (a.preventDefault(), /^____sdk____/.test(c))) {
          let b = JSON.parse(c.replace(/^____sdk____/, '')),
            d = b.command
          'APPSERVICE_INVOKE' === d &&
            ((this.syncDialogMap[b.data.callbackID] = a.dialog),
            this.props.assdkActions.exec(b.data))
        }
      })
    }
    setAppConfig() {
      e(this.props.project).then(a => {
        this.props.simulatorActions.setAppConfig(a)
      })
    }
    initProjectFileWatcher() {
      this.props.project &&
        !this.projectFiles &&
        d(this.props.project.projectpath).then(b => {
          ;(this.projectFiles = b),
            this.projectFiles.on('all', (b, c) => {
              let d = a.extname(c)
              '.json' === d &&
                process.nextTick(() => {
                  this.setAppConfig()
                })
            }),
            this.setAppConfig()
        })
    }
    onAppserviceWindowClose() {
      this.props.setDebuggerWindow({ show: !1 })
    }
    render() {
      let a = this.props
      return b.createElement(
        'div',
        { ref: 'container', style: { height: 0 } },
        b.createElement(i, null),
        a.show
          ? b.createElement(
              j,
              {
                title: '\u8C03\u8BD5\u5668',
                width: 500,
                height: 500,
                always_on_top: !0,
                onWindowClose: this.onAppserviceWindowClose.bind(this),
                ref: 'popupcontainer'
              },
              b.createElement('div', null)
            )
          : null
      )
    }
  }
  module.exports = k
})(require('lazyload'), require)
