'use strict'
var _extends =
  Object.assign ||
  function(a) {
    for (var b, c = 1; c < arguments.length; c++)
      for (var d in ((b = arguments[c]), b))
        Object.prototype.hasOwnProperty.call(b, d) && (a[d] = b[d])
    return a
  }
!(function(require, directRequire) {
  const a = require('react'),
    b = require('react-dom'),
    { Provider: c } = require('react-redux'),
    d = directRequire('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    e = require('./db2217eb4cff896bdcbc50abe005058f.js'),
    f = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    g = require('./da7c31daaf542cf1796023d8e344b98a.js'),
    h = require('./logger.js')
  class i extends a.Component {
    constructor(a) {
      super(a), (this.state = { mount: !0 })
    }
    render() {
      return this.state.mount
        ? a.createElement(
            'div',
            _extends({}, this.props, {
              style: { width: '100%', height: '100%' }
            }),
            this.props.children
          )
        : a.createElement('div', null)
    }
  }
  module.exports = class extends a.Component {
    constructor(a) {
      super(a), (this.queue = []), (this.container = void 0)
    }
    componentDidMount() {
      chrome.windows.getAll(f => {
        const j = {}
        f.forEach(a => {
          j[a.id] = !0
        }),
          nw.Window.open(
            this.props.templateHTML || 'html/popup.html',
            this.getWindowOptions(),
            f => {
              if (this.unmounted) return void f.close(!0)
              chrome.windows.getAll(a => {
                for (let b = 0; b < a.length; b++)
                  if (!j[a[b].id]) {
                    ;(global.windowChromeIdMap =
                      global.windowChromeIdMap || {})[a[b].id] = f
                    break
                  }
              }),
                (this.registryId =
                  this.props.registryId ||
                  `${this.props.title}_${+new Date()}`),
                (this.child = f),
                f.on('close', () => {
                  try {
                    f.hide()
                    const a = f.window.document.querySelectorAll('webview')
                    for (const b of a) b.style.display = 'none'
                  } catch (a) {}
                  try {
                    this.props.onWindowClose && this.props.onWindowClose(),
                      this.child &&
                        this.child.window.onWindowClose &&
                        this.child.window.onWindowClose()
                  } catch (a) {}
                  this.closeWindow()
                }),
                f.on('focus', () => {
                  global.__focusedWin = f
                }),
                global.windowMap.set(this.registryId, f)
              const k = global.Win.window,
                l = f.window
              if (((l.parent = k), !global.appConfig.isDev)) {
                function a(a, b) {
                  try {
                    let c = b.filename,
                      d = b.error.stack
                    h.error(`filename: ${c}, msg: ${d}`),
                      g(a, '', `filename: ${c}, msg: ${d}`)
                  } catch (a) {}
                }
                l.addEventListener('error', b => {
                  a('tool_error_web', b)
                })
              }
              const m = document.createElement('body'),
                n = document.createElement('div')
              if (
                ((n.style.cssText = 'width: 100%; height: 100%;'),
                this.props.className && (n.className = this.props.className),
                m.appendChild(n),
                (l.document.body = m),
                (this.container = n),
                m.addEventListener(
                  'dragover',
                  function(a) {
                    a.preventDefault(), a.stopPropagation()
                  },
                  !1
                ),
                m.addEventListener(
                  'drop',
                  function(a) {
                    a.preventDefault(), a.stopPropagation()
                  },
                  !1
                ),
                this.props.devtools)
              ) {
                if (this.queue && 0 < this.queue.length) {
                  this.child.window
                  this.queue.forEach(a => {
                    this.container.appendChild(a)
                  }),
                    (this.queue = [])
                }
                this.props.onReady && this.props.onReady(f)
              } else if (this.props.children)
                b.render(
                  a.createElement(
                    c,
                    { store: d },
                    a.createElement(
                      i,
                      _extends(
                        { ref: a => (this.dummy = a) },
                        this.props.wrapperProps
                      ),
                      this.props.children
                    )
                  ),
                  n,
                  () => {
                    this.registryId &&
                      e.register({ id: this.registryId, window: f }),
                      this.props.onReady && this.props.onReady(f)
                  }
                )
              else {
                const g = this.props.renderClass,
                  h = this.props.renderProps || {}
                b.render(
                  a.createElement(
                    c,
                    { store: d },
                    a.createElement(
                      i,
                      _extends(
                        { ref: a => (this.dummy = a) },
                        this.props.wrapperProps
                      ),
                      a.createElement(g, _extends({}, h, { win: f }))
                    )
                  ),
                  n,
                  () => {
                    this.registryId &&
                      (e.register({ id: this.registryId, window: f }),
                      this.props.onReady && this.props.onReady(f))
                  }
                )
              }
            }
          )
      })
    }
    componentWillUnmount() {
      ;(this.unmounted = !0), this.closeWindow()
    }
    closeWindow() {
      if (this.registryId)
        try {
          e.unregister(this.registryId)
        } catch (a) {}
      const a = () => {
        try {
          this.child.close(!0), (this.child = null)
        } catch (a) {}
      }
      if (this.dummy)
        try {
          this.dummy.setState({ mount: !1 }, () => {
            a()
          })
        } catch (b) {
          a()
        }
      else this.child && a()
    }
    appendChild(a) {
      if ((a && this.queue.push(a), !this.child)) return
      const b = this.child.window
      this.queue.forEach(a => {
        this.container
          ? this.container.appendChild(a)
          : b.document.body.appendChild(a)
      }),
        (this.queue = [])
    }
    getWindowOptions() {
      return function(a) {
        const b = {}
        for (let c of a) this.props.hasOwnProperty(c) && (b[c] = this.props[c])
        return (
          b.title ||
            ((b.title = nw.App.manifest.window.title),
            this.props.projectName &&
              (b.title = `${decodeURIComponent(this.props.projectName)} - ${
                b.title
              }`)),
          b.width || (b.width = f.SIZE.DEFAULT.WIDTH),
          b.height || (b.height = f.SIZE.DEFAULT.HEIGHT),
          b.hasOwnProperty('focus') || (b.focus = !0),
          b
        )
      }.bind(this)([
        'id',
        'title',
        'icon',
        'position',
        'focus',
        'always_on_top',
        'width',
        'height',
        'min_width',
        'min_height',
        'max_width',
        'max_height',
        'resizable',
        'kiosk',
        'fullscreen',
        'show_in_taskbar',
        'frame',
        'transparent'
      ])
    }
    render() {
      return a.createElement('div', { style: { display: 'none' } })
    }
  }
})(require('lazyload'), require)
