'use strict'
!(function(require, directRequire) {
  const a = require('path'),
    b = require('react'),
    c = require('./08daeb1466202b05ac4b7f58871f83db.js'),
    d = require('./d3976cc01aeebc5b09e11c4135b6bd8d.js'),
    e = require('./9fdd4ac31a05c27355910f0d74accd4c.js'),
    f = require('./ea653f45dc25181ca4f1b108175009b7.js'),
    g = require('./a8c87029da0fa06e986298d447ab0fe2.js'),
    h = require('./290634b065605c4034bdb091d12bb500.js'),
    i = require('classnames'),
    j = require('./ba23d8b47b1f4ea08b9fd49939b9443f.js'),
    k = 'about:blank',
    l = require('./logger.js'),
    m = require('./84705760a8692a44a583377e7f3f3b00.js'),
    n = require('./3bfffbe88b3d923921f851c0697974fe.js'),
    o = require('./2dfc6a3df6d6fc51266b293c8420e88b.js'),
    { connect: p } = require('react-redux')
  class q extends b.Component {
    constructor(a) {
      super(a),
        (this.syncDialogMap = {}),
        (this.restartTimer = null),
        (this.storagePannelShow = !1),
        (this.state = { heigth: a.heigth }),
        (this._onMessage = this.onMessage.bind(this))
    }
    componentDidMount() {
      o.initHelper({
        setDebuggerWindow: this.props.setDebuggerWindow.bind(this)
      }),
        c.register(this._onMessage),
        this.initWebview()
    }
    componentWillUnmount() {
      this.devtoolsview && this.devtoolsview.remove(),
        this.webview && this.webview.remove(),
        this.getWidgetDataTimer && clearTimeout(this.getWidgetDataTimer),
        c.unRegister(this._onMessage)
    }
    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)
      }
      if (a.jssdkCallbackInfo != this.props.jssdkCallbackInfo) {
        let { callbackID: b, res: d } = a.jssdkCallbackInfo
        this.syncDialogMap[b]
          ? (this.syncDialogMap[b].ok(JSON.stringify(d)),
            delete this.syncDialogMap[b])
          : c.invokeCallback(b, d)
      }
      ;(a.compileCommand != this.props.compileCommand ||
        a.device != this.props.device) &&
        (clearTimeout(this.restartTimer),
        (this.restartTimer = setTimeout(() => {
          this.restart()
        }))),
        a.errmsg != this.props.errmsg && a.errmsg && this.showConsole(a.errmsg),
        a.ready != this.props.ready &&
          a.ready &&
          setTimeout(() => {
            this.getWidgetData()
          })
    }
    showConsole(a) {
      if (/\?load$/.test(this.webview.src)) return void this.showConsole(a)
      let b = 0,
        c = setInterval(() => {
          let d = this.dpsWebview.src || ''
          ;/\?load$/.test(d)
            ? (this.showConsole(a), clearInterval(c))
            : (b++, 20 == b && clearInterval(c))
        }, 500)
    }
    showConsole(a) {
      if (this.webview)
        if (/\?load$/.test(this.webview.src))
          'string' == typeof a
            ? this.webview.executeScript({ code: `console.error(\`${a}\`)` })
            : this.webview.executeScript({
                code: `console.group(\`${a.group}\`)
              console.${a.type}(${
                  'string' == typeof a.msg
                    ? `\`${a.msg}\``
                    : JSON.stringify(a.msg)
                })
            console.groupEnd()
            `
              })
        else {
          let b = 0,
            c = setInterval(() => {
              let d = this.webview.src || ''
              ;/\?load$/.test(d)
                ? (this.showConsole(a), clearInterval(c))
                : (b++, 10 == b && clearInterval(c))
            }, 500)
        }
    }
    onMessage(a) {
      let { command: b, data: c } = a
      'APPSERVICE_INVOKE' == b && this.props.wssdkActions.exec(c)
    }
    initWebviewEvent(a) {
      a.addEventListener('dialog', a => {
        let b = a.messageType || '',
          c = a.messageText
        if ('prompt' !== b);
        else if ((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.wssdkActions.exec(b.data))
        }
      })
    }
    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(
        `wechatdevtools widgetservice port/${global.messageCenterPort}`
      ),
        b.setAttribute('partition', 'persist:appservice'),
        b.setAttribute('tabIndex', '-1'),
        this.initWebviewEvent(b)
      let c = `${a.getUserAgent()} widgetservicedevtools port/${
        global.messageCenterPort
      }`
      a.setUserAgentOverride(c),
        a.setAttribute('partition', 'persist:devtools'),
        a.setAttribute('style', 'height:100%;width:100%;position:relative;'),
        this.serviceContainer.appendChild(b),
        this.showDevTools()
    }
    showDevTools() {
      let a = this.devtoolsview,
        b = this.webview
      this.container.appendChild(a), this.hackNewWindow(a)
      const c = () => {
        a.removeEventListener('loadcommit', c)
        const d = () => {
          b.removeEventListener('loadcommit', d),
            b.showDevTools(!0, a),
            o.initWebview(b),
            (this.loaded = !0),
            this.props.simulatorActions.setAppLaunched(!0)
        }
        b.addEventListener('loadcommit', d), this.resetStatus(), (b.src = k)
      }
      a.addEventListener('loadcommit', c), (a.src = k)
    }
    resetStatus() {
      ;(this.loaded = !1), (this.couldGetData = !1)
    }
    hackNewWindow(a) {
      a.addEventListener('newwindow', function(a) {
        a.preventDefault()
        let b = a.targetUrl
        b &&
          ('https://developers.google.com/web/tools/chrome-devtools/' === b &&
            (b = 'https://mp.weixin.qq.com/debug/wxadoc/dev/index.html'),
          0 == b.indexOf('wxfile://') &&
            (b = b.replace('wxfile://', 'http://wxfile.open.weixin.qq.com/')),
          nw.Window.open(b, { width: 799, height: 799 }))
      })
    }
    restart() {
      if (!this.loaded || !this.webview) return
      let a = () => {
        this.webview.removeEventListener('loadcommit', a),
          (this.loaded = !0),
          (this.couldGetData = !0),
          this.props.simulatorActions.setAppLaunched(!0)
      }
      this.resetStatus(), this.webview.addEventListener('loadcommit', a)
      let b = this.props.project,
        c = `http://127.0.0.1:${global.proxyPort}/widgetservice/${
          b.compileType
        }`
      ;(this.webview.src = c),
        this.props.simulatorActions.setWidget({ ready: !1 })
    }
    onResizeStop(a, b, c) {
      this.props.windowActions.setDebuggerWindow({ height: c })
    }
    getWidgetData() {
      let a = this.props
      if (
        (a.compileType == e.conversation || a.compileType == e.search) &&
        this.props.ready &&
        this.couldGetData
      ) {
        this.getWidgetDataTimer &&
          (clearTimeout(this.getWidgetDataTimer),
          (this.getWidgetDataTimer = null))
        let b = { appid: n.getProjectAppID() }
        a.compileType == e.conversation
          ? (b.cachekey = a.condiction.cachekey)
          : a.compileType == e.search &&
            ((b.scene = 1),
            (b.query = a.wxaData),
            (b.cachekey = ''),
            (b.url = a.condiction.debugUrl),
            !b.query &&
              this.showConsole({
                group: '\u7F51\u7EDC\u9519\u8BEF',
                type: 'error',
                msg:
                  '\u83B7\u53D6\u5230\u7684\u641C\u7D22\u7ED3\u679C\u4E3A\u7A7A\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\u641C\u7D22\u6761\u4EF6\u540E\u91CD\u8BD5'
              })),
          m
            .getWidgetData(b)
            .then(a => {
              let b = 1e3 * a.interval || 5e3
              c.send({
                command: 'APPSERVICE_ON_EVENT',
                data: { eventName: 'onDataPush', data: { data: a } }
              }),
                (this.getWidgetDataTimer = setTimeout(() => {
                  this.getWidgetData()
                }, b))
            })
            .catch(a => {
              this.showConsole({
                group: '\u7F51\u7EDC\u9519\u8BEF',
                type: 'error',
                msg: `获取动态页数据失败: ${a}`
              })
            })
      }
    }
    render() {
      let a = this.props,
        c = { height: a.height }
      return (
        a.show && !a.editorShow && (c.flex = '1'),
        b.createElement(
          f,
          {
            innerRef: a => (this.container = a),
            width: '100%',
            height: a.height,
            className: i('devtools', { 'ui-invisible': !a.show }),
            style: c,
            topResizable: !0,
            onResizeStop: this.onResizeStop.bind(this)
          },
          b.createElement('div', {
            className: 'ui-divider ui-divider-horizontal',
            style: { pointerEvents: 'none' }
          }),
          b.createElement('div', {
            ref: a => (this.serviceContainer = a),
            style: { width: 0, height: 0 }
          })
        )
      )
    }
  }
  module.exports = p(
    a => {
      let b = a.toolbar.deviceInfo,
        c = a.project.current,
        d = c.compileType,
        e = c.condiction[d] || {},
        f = e.list[e.current] || {},
        g = a.simulator.widget || {}
      return {
        project: c,
        compileType: d,
        libVersion: c.libVersion,
        assdkCallbackInfo: a.simulator.assdkCallbackInfo,
        jssdkCallbackInfo: a.simulator.jssdkCallbackInfo,
        editorShow: a.window.editor && a.window.editor.show,
        show: a.window.debug.show,
        compileCommand: a.simulator.compileCommand,
        wxaData: g.wxaData || '',
        errmsg: g.errmsg || '',
        ready: g.ready,
        condiction: f,
        device: b
      }
    },
    a => {
      return {
        simulatorActions: d.bindActionCreators(j, a),
        windowActions: d.bindActionCreators(g, a),
        wssdkActions: d.bindActionCreators(h, a),
        setDebuggerWindow: d.bindActionCreators(g.setDebuggerWindow, a)
      }
    }
  )(q)
})(require('lazyload'), require)
