'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')
  class g extends a.Component {
    constructor(a) {
      super(a), (this.viewMap = {}), (this.callbackQueue = {})
    }
    componentDidMount() {
      this._onNativeViewMessage = this.onNativeViewMessage.bind(this)
    }
    componentWillReceiveProps(a) {
      a.height == this.props.height && a.width == this.props.width,
        a.compileCommand != this.props.compileCommand && this.restart(),
        a.operateNativeView != this.props.operateNativeView &&
          this.operateNativeView(a.operateNativeView)
    }
    componentWillUnmount() {
      this.removeAllView()
    }
    restart() {
      this.removeAllView()
    }
    operateNativeView(a) {
      if (a)
        switch (a.action) {
          case 'insert': {
            this.insertView(a)
            break
          }
          case 'update': {
            this.updateView(a)
            break
          }
          case 'remove': {
            this.removeView(a)
            break
          }
          case 'operate':
            this.operateView(a)
        }
    }
    insertView(a) {
      if (!a || 'insert' != a.action) return
      if (
        !a.position ||
        !a.position.hasOwnProperty('left') ||
        !a.position.hasOwnProperty('top') ||
        !a.position.hasOwnProperty('width') ||
        !a.position.hasOwnProperty('height')
      )
        return void this.props.assdkCallback({
          callbackID: a.callbackID,
          res: { errMsg: `${a.api}:fail param position error` }
        })
      let { left: e, top: f, width: g, height: h } = a.position,
        i = this.props.deviceScale
      ;(g /= i), (h = g / i)
      let j = `${a.name}${a.id}`
      if (this.viewMap[j])
        return void this.props.assdkCallback({
          callbackID: a.callbackID,
          res: { errMsg: `${a.api}:fail already exist` }
        })
      let k = (this.webview = b.get('simulator', {
        width: g,
        height: h,
        dpr: this.props.dpr
      }))
      ;(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;`
        ),
        (a.data.id = j),
        (a.data.videoPlayerId = a.id)
      var l = { 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.viewMap[j] = k),
        (this.callbackQueue[j] = a)
    }
    removeView(a) {
      if (a && 'remove' == a.action) {
        let b = `${a.name}${a.id}`,
          c = this.viewMap[b]
        return c
          ? void (c.detach(),
            d.unRegister(b, this._onNativeViewMessage),
            delete this.viewMap[b],
            this.props.assdkCallback({
              callbackID: a.callbackID,
              res: { errMsg: `${a.api}:ok` }
            }))
          : void this.props.assdkCallback({
              callbackID: a.callbackID,
              res: { errMsg: `${a.api}:fail not found` }
            })
      }
    }
    updateView(a) {
      if (a && 'update' == a.action) {
        let b = `${a.name}${a.id}`,
          c = this.viewMap[b]
        if (!c)
          return void this.props.assdkCallback({
            callbackID: a.callbackID,
            res: { errMsg: `${a.api}:fail not found` }
          })
        if (a.position) {
          let { left: b, top: d, width: e, height: f } = a.position,
            g = this.props.deviceScale
          ;(e /= g),
            (f = e / g),
            c.setAttribute(
              'style',
              `position:absolute;left:${b}px;top:${d}px;height:${f}px;width:${e}px;pointer-events:initial;`
            )
        }
        d.triggerOnEvent({
          webviewID: b,
          eventName: 'updateNativeView',
          data: a.data
        }),
          this.props.assdkCallback({
            callbackID: a.callbackID,
            res: { errMsg: `${a.api}:ok` }
          })
      }
    }
    operateView(a) {
      if (a && 'operate' == a.action) {
        let b = `${a.name}${a.id}`,
          c = this.viewMap[b]
        return c
          ? void (d.triggerOnEvent({
              webviewID: b,
              eventName: 'operateNativeView',
              data: a.data
            }),
            this.props.assdkCallback({
              callbackID: a.callbackID,
              res: { errMsg: `${a.api}:ok` }
            }))
          : void this.props.assdkCallback({
              callbackID: a.callbackID,
              res: { errMsg: `${a.api}:fail not found` }
            })
      }
    }
    onNativeViewMessage(a) {
      let b = {}
      try {
        if (((b = JSON.parse(a)), 'WEBVIEW_PUBLISH' == b.command)) {
          let a = b.data.eventName,
            c = b.fromWebviewID,
            d = this.viewMap[c]
          if ('videoPlayerInsert' != a)
            'onNativeViewEvent' == a &&
              e.triggerOnEvent({
                eventName: b.data.data.eventName,
                data: b.data.data.data
              })
          else if (this.callbackQueue[c]) {
            let a = this.callbackQueue[c]
            this.props.assdkCallback({
              callbackID: a.callbackID,
              res: { errMsg: `${a.api}:ok` }
            }),
              delete this.callbackQueue[c]
          }
        }
      } catch (b) {
        return void f.error(`onNativeViewMessage catch ${a} error ${b}`)
      }
    }
    removeAllView() {
      for (let a in this.viewMap)
        this.viewMap[a] &&
          (this.viewMap[a].detach(), d.unRegister(a, this._onNativeViewMessage))
      this.viewMap = {}
    }
    render() {
      let b = this.props,
        c = {
          height: b.height,
          width: b.width,
          position: 'absolute',
          pointerEvents: 'none'
        }
      return a.createElement('div', {
        className: 'webview',
        ref: a => (this.container = a),
        style: c
      })
    }
  }
  module.exports = g
})(require('lazyload'), require)
