'use strict'
!(function(require, directRequire) {
  const a = require('react'),
    b = require('./a78e6d6a87de1708226375ca4c320d76.js'),
    c = require('querystring'),
    d = require('./6b70fc1a2dbee232784064c344aaaf2b.js'),
    e = require('./e3681b47a6ce46a8998b8cdff40bdb12.js'),
    f = require('./logger.js'),
    g = require('redux'),
    h = require('./d3976cc01aeebc5b09e11c4135b6bd8d.js'),
    i = require('./ba23d8b47b1f4ea08b9fd49939b9443f.js'),
    { connect: j } = require('react-redux')
  class k extends a.Component {
    constructor(a) {
      super(a)
    }
    componentDidMount() {
      ;(this._onNativeViewMessage = this.onNativeViewMessage.bind(this)),
        this.insertView(this.props.node)
    }
    componentWillReceiveProps(a) {
      a.operateNativeView != this.props.operateNativeView &&
        a.operateNativeView.id == a.node.id &&
        a.operateNativeView.name == a.node.name &&
        setTimeout(() => {
          this.operateNativeView(this.props.operateNativeView)
        }),
        a.node != this.props.node &&
          setTimeout(() => {
            this.updateView(this.props.node)
          })
    }
    componentWillUnmount() {
      d.unRegister(this.key, this._onNativeViewMessage)
    }
    operateNativeView(a) {
      if (a)
        switch (a.action) {
          case 'operate':
            this.operateView(a)
        }
    }
    insertView(a) {
      let { left: e, top: f, width: g, height: h } = a.position,
        i = this.props.deviceScale
      ;(g /= i), (h = g / i)
      let j = (this.key = `${a.name}${a.id}`),
        k = (this.webview = b.get('simulator', {
          width: g,
          height: h,
          dpr: this.props.dpr
        }))
      ;(this.lastWidth = g),
        (this.lastHeight = h),
        (k.needDebugger = 1),
        k.setUserAgentOverride(
          `wechatdevtools port/${global.messageCenterPort} gamenativeview/${j}`
        ),
        k.setAttribute('allowtransparency', !0),
        k.setAttribute(
          'style',
          `position:absolute;left:${e}px;top:${f}px;height:${h}px;width:${g}px;pointer-events:initial;`
        )
      var l = {
        videoPlayerId: a.id,
        data: JSON.stringify(a.data),
        name: a.name
      }
      ;(k.src = `http://127.0.0.1:${
        global.proxyPort
      }/game/nativeview.html?${c.stringify(l)}`),
        k.attach(this.container),
        d.register(j, this._onNativeViewMessage),
        (this.webview = k)
    }
    removeView() {
      this.webview && (this.webview.detach(), (this.webview = void 0)),
        d.unRegister(this.key, this._onNativeViewMessage)
    }
    updateView(a) {
      if (a.position && this.webview) {
        let { left: b, top: c, width: d, height: e } = a.position,
          f = this.props.deviceScale
        ;(d /= f),
          (e /= f),
          this.webview.setAttribute(
            'style',
            `position:absolute;left:${b}px;top:${c}px;height:${e}px;width:${d}px;pointer-events:initial;`
          ),
          (this.lastHeight != e || this.lastWidth != d) &&
            ((this.lastWidth = d),
            (this.lastHeight = e),
            this.webview.setOffset({
              width: d,
              height: e,
              dpr: this.props.dpr
            }))
      }
      d.triggerOnEvent({
        webviewID: this.key,
        eventName: 'updateNativeView',
        data: a.data
      })
    }
    operateView(a) {
      a &&
        'operate' == a.action &&
        d.triggerOnEvent({
          webviewID: this.key,
          eventName: 'operateNativeView',
          data: a.data
        })
    }
    onNativeViewMessage(a) {
      let b = {}
      try {
        if (((b = JSON.parse(a)), 'WEBVIEW_PUBLISH' == b.command)) {
          let a = b.data.eventName
          'videoPlayerInsert' == a
            ? this.props.assdkCallback({
                callbackID: this.props.node.callbackID,
                res: { errMsg: `${this.props.node.api}:ok` }
              })
            : 'onNativeViewEvent' == a &&
              e.triggerOnEvent({
                eventName: b.data.data.eventName,
                data: b.data.data.data
              })
        }
      } catch (b) {
        return void f.error(`onNativeViewMessage catch ${a} error ${b}`)
      }
    }
    render() {
      let b = this.props
      return a.createElement('div', {
        ref: a => (this.container = a),
        style: {}
      })
    }
  }
  module.exports = j(
    a => {
      let b = a.toolbar.deviceInfo,
        c =
          a.simulator.orientation &&
          'landscape' == a.simulator.orientation.value,
        d = a.simulator.operateNativeView,
        e = c ? b.screenWidth : b.screenHeight
      return {
        width: c ? b.screenHeight : b.screenWidth,
        height: e,
        deviceScale: a.toolbar.deviceScale,
        dpr: b.dpr,
        compileCommand: a.simulator.compileCommand,
        project: a.project.current,
        operateNativeView: d
      }
    },
    a => {
      return { assdkCallback: h.bindActionCreators(i.assdkCallback, a) }
    }
  )(k)
})(require('lazyload'), require)
