'use strict'
!(function(require, directRequire) {
  const a = require('react'),
    b = require('./e40e098fcb2111ba9f21e79c183fec7e.js'),
    c = require('./4762f28cad4425c4fc55dec009c87a7e.js'),
    d = require('./a78e6d6a87de1708226375ca4c320d76.js'),
    e = 'html/about.html'
  class f extends a.Component {
    constructor(a) {
      super(a),
        (this.webviewID = `${Date.now()}${parseInt(1e4 * Math.random())}`)
    }
    componentDidMount() {
      this.initWebview(),
        (this._onMessage = this.onMessage.bind(this)),
        b.register(this._onMessage),
        global.Win.setResizable(!0)
    }
    componentWillReceiveProps(a) {
      if (
        (a.device != this.props.device &&
          setTimeout(() => {
            this.setWebviewInfo()
          }),
        a.act != this.props.act)
      ) {
        let b = this.props.act,
          c = a.act
        setTimeout(() => {
          this.doActions(c, b)
        })
      }
      if (a.jssdkCallbackInfo != this.props.jssdkCallbackInfo) {
        const c = a.jssdkCallbackInfo.runtimeID
        c
          ? b.directSend(
              {
                command: 'WEBDEBUGGER_INVOKE_CALLBACK',
                data: a.jssdkCallbackInfo
              },
              c
            )
          : b.send({
              command: 'WEBDEBUGGER_INVOKE_CALLBACK',
              data: a.jssdkCallbackInfo
            })
      }
    }
    componentWillUnmount() {
      b.unRegister(this._onMessage)
    }
    onMessage(a) {
      let { command: b, data: c, webviewID: d, runtimeID: e } = a
      d != this.webviewID ||
        ('WEBDEBUGGER_INVOKE' == b
          ? this.props.jssdkActions.exec(c, e)
          : 'WEBDEBUGGER_GET_TITLE_RES' == b &&
            this.props.debuggerActions.setTitle(c.title))
    }
    doActions(a, b) {
      let c = this.webview
      return c
        ? a.reload == b.reload
          ? a.forward == b.forward
            ? a.back == b.back
              ? a.blank == b.blank
                ? void (
                    a.load != b.load &&
                    ((this.needGetA8key = 'urlbar' !== this.props.from),
                    (c.src =
                      'about:blank' == this.props.url ? e : this.props.url))
                  )
                : void (c.src = e)
              : void (c.canGoBack() && c.back())
            : void c.forward()
          : void c.reload()
        : void 0
    }
    initWebview() {
      const a = this.props
      let b = (this.webview = d.get('htmlwebview', {
        width: a.width,
        height: a.height,
        dpr: a.dpr
      }))
      ;(b.className = `simulator-bd-webview_body webviewbody`),
        b.attach(this.container),
        this.setWebviewInfo(),
        this.initEvent(),
        this.props.debuggerActions.showDevtools(b._webview)
    }
    initEvent() {
      let a = this.webview
      ;(a.onBeforeRequest = a => {
        let { url: b, type: c } = a
        if ('main_frame' !== c) return {}
        if (/^chrome-extension:\/\//.test(b) || /^file:\/\//.test(b)) return {}
        if (!this.needGetA8key) return (this.needGetA8key = !0), {}
        let d = /\#wechat_redirect$/.test(b)
        return (
          this.props.debuggerActions.getA8Key({
            url: b,
            isSync: d,
            from: 'webviewrequest'
          }),
          { cancel: d }
        )
      }),
        a.on('loadstart', a => {
          a.isTopLevel && this.upWebviewStatus({ loading: 'start' })
        }),
        a.on('loadcommit', a => {
          a.isTopLevel && this.upWebviewStatus({ type: 'loadcommit' })
        }),
        a.on('loadstop', () => {
          this.upWebviewStatus({ loading: 'stop' }),
            b.send({ command: 'WEBDEBUGGER_GET_TITLE' })
        }),
        a.on('dialog', a => {
          let b = a.messageType || '',
            c = a.messageText,
            d = a.dialog
          if ('alert' === b)
            this.props.debuggerActions.setConfirm({
              show: !0,
              showCancel: !1,
              content: c
            })
          else if ('confirm' === b)
            a.preventDefault(),
              this.props.debuggerActions.setConfirm({
                show: !0,
                showCancel: !0,
                content: c,
                callback: a => {
                  a ? d.ok() : d.cancel()
                }
              })
          else if ('prompt' === b) {
            let a = prompt(c)
            null === a ? d.cancel() : d.ok(a)
          }
        }),
        a.on('mouseleave', () => {
          c.send({ command: 'SET_TOUCH_MODE', data: !1 })
        }),
        a.on('mouseenter', () => {
          c.send({ command: 'SET_TOUCH_MODE', data: !0 })
        })
    }
    setWebviewInfo() {
      let a = this.webview,
        b = this.props.height / this.props.deviceScale,
        c = this.props.width / this.props.deviceScale
      a.setUserAgentOverride(
        `${this.props.ua.replace(
          '{{webviewID}}',
          this.webviewID
        )} webdebugger port/${global.messageCenterPort}`
      ),
        a.setAttribute('style', `position:absolute;height:${b}px;width:${c}px`),
        a.reload()
    }
    upWebviewStatus() {
      let a = this.webview.src,
        b = this.webview.canGoBack()
      this.props.leftbtn != b && this.props.debuggerActions.setLeftBtn(b)
      a == this.props.url || this.props.debuggerActions.setUrl(a)
    }
    render() {
      let b = {
        width: this.props.width / this.props.deviceScale,
        height: this.props.height / this.props.deviceScale
      }
      return a.createElement('div', {
        className: 'webview',
        ref: a => (this.container = a),
        style: b
      })
    }
  }
  module.exports = f
})(require('lazyload'), require)
