'use strict'
!(function(require, directRequire) {
  const a = require('react'),
    b = require('redux'),
    c = require('./f0466135fc8b3a662084784e5f4ac792.js'),
    d = require('./eadce02c750c875a10680bcfedadec88.js'),
    e = require('./9fdd4ac31a05c27355910f0d74accd4c.js'),
    f = require('./1fcc6bd55b687d154a4247e57fe3011d.js'),
    g = require('./2f0aa63ae61d48294b5e9927ab57e1a4.js'),
    h = require('./fc137838572a83604db39acff8e909e0.js'),
    i = require('./cc2c2970ff81ae4a83123e81ee123da2.js'),
    j = require('./d3976cc01aeebc5b09e11c4135b6bd8d.js'),
    k = require('moment'),
    l = require('./63f52c9055d92f52ca0510da7d3dcadb.js'),
    m = require('./a8c87029da0fa06e986298d447ab0fe2.js'),
    n = require('./e4d68def70709dc877cb2ba5bdad18cb.js'),
    o = require('./e98c60a262d8d98e69e574a9d12a21df.js'),
    { getAvailablePort: p } = require('./tools.js'),
    q = (() => {
      let a
      return async function(b = !1) {
        return !b && a
          ? Promise.resolve(a)
          : ((a = await p()), Promise.resolve(a))
      }
    })(),
    { connect: r } = require('react-redux')
  class s extends a.Component {
    constructor(a) {
      k.locale(navigator.language),
        super(a),
        (this.state = {
          loading: !0,
          show: a.show,
          src: '',
          tipsShow: !1,
          previewToolShown: !1,
          filesIgnored: [],
          remoteDebugEvents: [],
          filesMissing: []
        }),
        (this.previewTimer = null),
        (this._mounted = !1)
    }
    componentDidMount() {
      ;(this._mounted = !0),
        this.props.compilerSettings.liveReload || this.loadQRCode()
    }
    loadQRCode() {
      this.setState({
        loading: !0,
        filesIgnored: [],
        filesMissing: [],
        tipsShow: !1,
        stage: null,
        src: '',
        remoteDebugStatus: null,
        remoteDebugEvents: []
      }),
        (this.previewTimer = setTimeout(async () => {
          let a
          const b = (a = null, b = '') => {
              console.log('progress stage', a, 'detailedText', b),
                this.setState({ progressDetailedText: b, stage: a })
            },
            f = this.props,
            g = f.project
          if (
            g &&
            g.setting &&
            g.setting.scriptsEnable &&
            g.scripts &&
            g.scripts.beforePreview
          ) {
            b(
              'custombeforepreview',
              '\u6B63\u5728\u6267\u884C\u81EA\u5B9A\u4E49\u9884\u89C8\u524D\u547D\u4EE4...'
            ),
              f.consoleActions.clear(),
              f.consoleActions.log(
                '\u5F00\u59CB\u6267\u884C\u81EA\u5B9A\u4E49\u9884\u89C8\u524D\u9884\u5904\u7406\u547D\u4EE4...'
              )
            try {
              await n.run({
                command: g.scripts.beforePreview,
                options: { cwd: g.projectpath, shell: !0 },
                stderr: f.consoleActions.error,
                stdout: f.consoleActions.log
              })
            } catch (a) {
              return void f.consoleActions.error(
                '\u6267\u884C\u81EA\u5B9A\u4E49\u9884\u89C8\u524D\u9884\u5904\u7406\u547D\u4EE4\u5931\u8D25!'
              )
            }
            b(
              'custombeforepreview',
              '\u81EA\u5B9A\u4E49\u9884\u89C8\u524D\u547D\u4EE4\u6267\u884C\u5B8C\u6BD5'
            )
          }
          const h = f.remoteDebug && global.l ? await q(!0) : void 0
          let i = {
            test: !0,
            onProgressUpdate: b,
            onFilesIgnored: (a = []) => {
              this.setState({ filesIgnored: a })
            },
            onFilesMissing: (a = []) => {
              this.setState({ filesMissing: a })
            },
            remoteDebug: f.remoteDebug,
            remoteDebugLocal: global.l,
            remoteProxyPort: h,
            disableUrlCheck: f.remoteDebugWindow.disableUrlCheck,
            liveReload: f.compilerSettings.liveReload
          }
          try {
            let b = this.props.project,
              c = b.compileType,
              d = b.condiction[c]
            ;(a = d.list[d.current]),
              c == e.search
                ? a &&
                  ((i.searchQuery = a.customQuery || a.query),
                  (i.boxQI = a.customQuery ? '' : a.boxQI))
                : (c == e.weapp || c == e.plugin) &&
                  ((i.page = a ? a.pathName : this.props.appConfig.pages[0]),
                  (i.query = a ? a.query : '')),
              b.attr.gameApp && (i.query = a ? a.query : '')
          } catch (a) {}
          c(i)
            .then(async a => {
              if (this._mounted) {
                let c = +new Date() + 1500000,
                  e = k(c).calendar(),
                  g = k(new Date()).calendar()
                if (f.remoteDebug) {
                  const {
                    wxpkg_info: c = '',
                    room_id: e = '',
                    qrcode_img: g = '',
                    username: i = ''
                  } = a
                  if (!c || !e || !g) throw new Error('invalid request')
                  let j = await l.init(
                    f.project,
                    {
                      wxpkg_info: c,
                      room_id: e,
                      username: i,
                      appid: f.project.appid
                    },
                    {
                      usingLocalStorage: f.remoteDebugWindow.usingLocalStorage,
                      remoteDebugLocal: global.l,
                      clientProxyPort: h,
                      onStatusUpdate: (b, c) => {
                        this.state.show &&
                          (c && j && j.url
                            ? (this.props.windowActions.setRemoteDebugWindow({
                                show: !0,
                                inspectUrl: j.url,
                                serverInfo: a
                              }),
                              this.setState({ show: !1 }),
                              this.props.toggleClickKey(d.NONE))
                            : this.setState({ remoteDebugStatus: b }))
                      },
                      onProgressUpdate: b,
                      onEvent: a => {
                        this.state.show &&
                          this.setState({
                            remoteDebugEvents: (
                              this.state.remoteDebugEvents || []
                            ).concat(a)
                          })
                      }
                    }
                  )
                }
                this.setState({
                  generateTime: g,
                  qrcode_time: e,
                  src: `data:image/png;base64,${a.qrcode_img}`,
                  loading: !1,
                  progressDetailedText: '',
                  stage: null
                }),
                  this.props.setPkgSize({
                    preview: a.wxpkg_size,
                    previewSubpackages: a.subpackage_info,
                    lastPreviewTime: +new Date()
                  })
              }
            })
            .catch(a => {
              this._mounted &&
                (this.props.showError(a instanceof Error ? a : new Error(a)),
                this.setState({ show: !1 }),
                this.props.toggleClickKey(d.NONE))
            })
        }))
    }
    componentWillReceiveProps(a) {
      a.show != this.props.show && this.setState({ show: a.show }),
        !a.show ||
          this.props.compilerSettings.liveReload ||
          a.remoteDebug ||
          a.timeStamp === this.props.timeStamp ||
          this.loadQRCode()
    }
    componentWillUnmount() {
      ;(this._mounted = !1),
        clearTimeout(this.previewTimer),
        n.abort(),
        this.props.remoteDebug &&
          !this.props.remoteDebugWindow.show &&
          (null !== this.state.remoteDebugStatus && l.destroy(),
          this.props.project.projectid ===
            this.props.remoteDebugWindow.debuggingProjectId &&
            this.props.windowActions.setRemoteDebugWindow({
              debuggingProjectId: null
            }))
    }
    onHandleClick(a) {
      a.stopPropagation()
    }
    onCopyClick(a, b) {
      b.stopPropagation()
      let c = this.imgdom
      'preview' === a && (c = this.previewImgDom)
      let { left: d, top: e, height: f, width: g } = c.getBoundingClientRect()
      const h = () => {
        let a = global.windowMap.get('MAIN'),
          b = a.window.document.body.offsetWidth
        a.capturePage(
          a => {
            let h = document.createElement('canvas'),
              c = h.getContext('2d')
            ;(h.height = f), (h.width = g)
            let i = new Image()
            ;(i.onload = () => {
              let a = i.width / b
              c.drawImage(i, d * a, e * a + 5, g * a, f * a - 5, 0, 0, g, f)
              let j = nw.Clipboard.get()
              j.set(h.toDataURL(), 'png'),
                this.props.showSuccessTip('\u590D\u5236\u6210\u529F')
            }),
              (i.src = a)
          },
          { format: 'png', datatype: 'datauri' }
        )
      }
      this._blinkTimeout ||
        ((c.style.opacity = 0.6),
        (this._blinkTimeout = setTimeout(() => {
          ;(c.style.opacity = 1),
            (this._blinkTimeout = null),
            setTimeout(() => h(), 20)
        }, 200)))
    }
    handleIconAskClick(a) {
      a.stopPropagation()
      try {
        nw.Shell.openExternal(
          'https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/mydev.html'
        )
      } catch (a) {
        console.warn('open external failed.')
      }
    }
    handleWarningAskClick(a) {
      a.stopPropagation()
      try {
        nw.Shell.openExternal(
          'https://developers.weixin.qq.com/miniprogram/dev/devtools/debug.html#%E7%BC%96%E8%AF%91%E5%BC%82%E5%B8%B8%E4%BF%A1%E6%81%AF'
        )
      } catch (a) {
        console.warn('open external failed.')
      }
    }
    showScanCode(a) {
      a.stopPropagation(), this.setState({ previewToolShown: !1 })
    }
    showPreviewTool(a) {
      a.stopPropagation(), this.setState({ previewToolShown: !0 })
    }
    handleTipsClick(a) {
      a.stopPropagation()
      const b = this.state.tipsShow
      let c = this.props.top + 20
      this.setState({ tipsShow: !b, tipsTop: c })
    }
    handleTipsLinkClick(a) {
      a.stopPropagation()
      try {
        nw.Shell.openExternal(
          'https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=000e0696f084e0b0f21613f6a51804'
        )
      } catch (a) {
        console.warn('open external failed.')
      }
    }
    handleLiveReloadClick() {}
    render() {
      const b = this.props
      return a.createElement(
        'div',
        null,
        a.createElement(
          'div',
          {
            className: 'preview-group',
            style: {
              position: 'absolute',
              top: this.props.top,
              left: this.props.left,
              display: this.state.show ? '' : 'none',
              width: '252px'
            }
          },
          a.createElement(
            'div',
            { className: 'preview-item' },
            a.createElement(
              'div',
              {
                className: 'ui-popover',
                style: { position: 'relative', width: '252px' },
                onClick: this.onHandleClick.bind(this)
              },
              a.createElement(
                'div',
                { className: 'previewer' },
                a.createElement(
                  'div',
                  {
                    className: 'previewer-header',
                    onClick: this.showScanCode.bind(this),
                    style: { cursor: 'pointer' }
                  },
                  a.createElement(
                    'h4',
                    null,
                    b.remoteDebug
                      ? '\u8FDC\u7A0B\u8C03\u8BD5'
                      : '\u626B\u7801\u9884\u89C8'
                  )
                ),
                a.createElement(
                  'div',
                  {
                    ref: a => (this.imgdom = a),
                    style: {
                      position: 'relative',
                      display: this.props.compilerSettings.liveReload
                        ? 'none'
                        : ''
                    }
                  },
                  a.createElement(
                    'div',
                    {
                      className: 'previewer-body',
                      style: this.state.previewToolShown
                        ? { display: 'none' }
                        : {}
                    },
                    a.createElement(
                      'div',
                      { className: 'previewer-qrcode' },
                      a.createElement(
                        'div',
                        {
                          className: 'preview-qrcode-title',
                          style:
                            this.state.loading || b.remoteDebug
                              ? { display: 'none' }
                              : {}
                        },
                        a.createElement(
                          'p',
                          null,
                          this.props.nickName,
                          ' \u4E8E ',
                          this.state.generateTime || '\u672A\u77E5\u65F6\u95F4',
                          ' \u4E0A\u4F20'
                        )
                      ),
                      !this.state.loading &&
                        0 <
                          (this.state.filesIgnored || []).length +
                            (this.state.filesMissing || []).length &&
                        a.createElement(
                          'p',
                          {
                            style: {
                              fontWeight: 600,
                              marginBottom: '10px',
                              textAlign: 'center'
                            }
                          },
                          a.createElement(
                            'a',
                            {
                              style: { color: '#586c94' },
                              onClick: this.handleTipsClick.bind(this)
                            },
                            '\u672C\u6B21\u7F16\u8BD1\u5F02\u5E38\uFF1A',
                            (this.state.filesIgnored || []).length +
                              (this.state.filesMissing || []).length
                          )
                        ),
                      a.createElement('img', {
                        onClick: this.onCopyClick.bind(this, 'qrcode'),
                        src: this.state.src
                          ? this.state.src
                          : '../static/image/qrcode.png',
                        style: {
                          minHeight: '200px',
                          visibility: this.state.src ? 'visible' : 'hidden'
                        }
                      }),
                      a.createElement(
                        'div',
                        {
                          className: 'previewer-loading',
                          style: this.state.loading
                            ? { backgroundColor: 'transparent' }
                            : { display: 'none' }
                        },
                        a.createElement(
                          'div',
                          { className: 'previewer-loading-content' },
                          a.createElement('i', {
                            className: 'ui-icon-spinner'
                          }),
                          a.createElement(
                            'p',
                            { className: 'ui-desc' },
                            this.state.progressDetailedText
                          )
                        )
                      ),
                      0 <
                        (this.state.filesIgnored || []).length +
                          (this.state.filesMissing || []).length &&
                        a.createElement(
                          'div',
                          {
                            className: 'preview-qrcode-tips',
                            style: this.state.loading
                              ? { backgroundColor: 'transparent' }
                              : { display: 'none' }
                          },
                          a.createElement(
                            'p',
                            null,
                            a.createElement(
                              'a',
                              { onClick: this.handleTipsClick.bind(this) },
                              a.createElement('i', {
                                className: 'ui-icon-circle-x'
                              }),
                              ' ',
                              a.createElement(
                                'span',
                                null,
                                ' \u53D1\u73B0\u5F02\u5E38\uFF1A',
                                (this.state.filesIgnored || []).length +
                                  (this.state.filesMissing || []).length
                              ),
                              a.createElement('i', {
                                className: 'ui-icon-arrow-right-o'
                              })
                            )
                          )
                        ),
                      a.createElement(
                        'div',
                        { className: 'preview-qrcode-tips' },
                        a.createElement(
                          'p',
                          {
                            style: {
                              display: this.state.qrcode_time ? '' : 'none'
                            }
                          },
                          a.createElement('span', null, this.state.qrcode_time),
                          ' \u65F6\u5931\u6548'
                        )
                      )
                    )
                  )
                ),
                a.createElement(
                  'div',
                  null,
                  a.createElement(
                    'pre',
                    {
                      style: {
                        margin: '10px',
                        textAlign: 'left',
                        fontSize: '11px'
                      }
                    },
                    this.props.autoUploadFailureText || ''
                  ),
                  a.createElement(
                    'div',
                    { style: { marginTop: '10px', marginBottom: '10px' } },
                    a.createElement(
                      'label',
                      {
                        htmlFor: '',
                        className: 'ui-checkbox',
                        onClick: this.handleLiveReloadClick.bind(this),
                        style: {
                          display: 'none',
                          opacity:
                            !this.props.compilerSettings.liveReload &&
                            this.state.loading
                              ? '0.3'
                              : '1',
                          cursor:
                            !this.props.compilerSettings.liveReload &&
                            this.state.loading
                              ? 'default'
                              : 'pointer'
                        }
                      },
                      a.createElement('i', {
                        className: this.props.compilerSettings.liveReload
                          ? 'ui-icon-checkbox-o'
                          : 'ui-icon-checkbox'
                      }),
                      a.createElement(
                        'span',
                        { className: 'ui-checkbox-text' },
                        '\u7F16\u8BD1\u65F6\u624B\u673A\u81EA\u52A8\u5237\u65B0'
                      )
                    )
                  )
                ),
                a.createElement(
                  'p',
                  null,
                  (() => {
                    const c = this.state.remoteDebugStatus
                    if (!b.remoteDebug) return null
                    if ('number' != typeof c) return null
                    return -1 === c
                      ? a.createElement(
                          'span',
                          { style: { color: 'red', fontWeight: '600' } },
                          '\u5DF2\u7ED3\u675F\uFF0C\u8BF7\u91CD\u65B0\u5F00\u59CB'
                        )
                      : 0 === c
                        ? a.createElement(
                            'span',
                            { style: { color: 'red', fontWeight: '600' } },
                            '\u672A\u521D\u59CB\u5316'
                          )
                        : 1 === c
                          ? a.createElement(
                              'span',
                              { style: { color: 'red', fontWeight: '600' } },
                              '\u672A\u8FDE\u63A5'
                            )
                          : 2 === c
                            ? a.createElement(
                                'span',
                                { style: { color: 'red', fontWeight: '600' } },
                                '\u5DF2\u767B\u51FA'
                              )
                            : 3 === c
                              ? a.createElement(
                                  'span',
                                  { style: { fontWeight: '600' } },
                                  '\u6B63\u5728\u767B\u9646'
                                )
                              : 4 === c
                                ? '\u7B49\u5F85\u8BBE\u5907'
                                : 5 === c
                                  ? a.createElement(
                                      'span',
                                      { style: { fontWeight: '600' } },
                                      '\u670D\u52A1\u5668\u963B\u585E'
                                    )
                                  : 6 === c
                                    ? '\u7B49\u5F85\u56DE\u5305'
                                    : 7 === c
                                      ? '\u6B63\u5E38'
                                      : a.createElement(
                                          'span',
                                          {
                                            style: {
                                              color: 'red',
                                              fontWeight: '600'
                                            }
                                          },
                                          '\u672A\u77E5'
                                        )
                  })()
                ),
                (this.state.remoteDebugEvents || []).map((b, c) => {
                  return a.createElement(
                    'p',
                    { key: c, style: { color: 'gray', fontSize: '11px' } },
                    b.type + ', ' + (b.message || '')
                  )
                }),
                a.createElement(
                  'div',
                  {
                    className: 'preview-footer',
                    style: this.state.previewToolShown
                      ? { display: 'none' }
                      : {}
                  },
                  !this.props.compilerSettings.liveReload &&
                    !this.state.stage &&
                    !b.remoteDebug &&
                    a.createElement(
                      'div',
                      null,
                      a.createElement(
                        'button',
                        {
                          onClick: this.onCopyClick.bind(this, 'qrcode'),
                          className: 'ui-button ui-button-default'
                        },
                        '\u590D\u5236\u4E8C\u7EF4\u7801'
                      )
                    )
                )
              )
            ),
            this.state.tipsShow &&
              a.createElement(
                'div',
                {
                  onClick: this.onHandleClick.bind(this),
                  className: 'ui-popover',
                  style: { position: 'absolute', left: '252px' }
                },
                a.createElement(
                  'div',
                  { className: 'previewer' },
                  a.createElement(
                    'div',
                    { className: 'previewer-header' },
                    a.createElement(
                      'h4',
                      null,
                      '\u5F02\u5E38\u4FE1\u606F ',
                      a.createElement('i', {
                        className: 'ui-icon-ask',
                        style: { cursor: 'pointer' },
                        title: '\u70B9\u51FB\u67E5\u770B\u8BE6\u60C5',
                        onClick: this.handleWarningAskClick.bind(this)
                      })
                    )
                  ),
                  a.createElement(
                    'div',
                    { className: 'previewer-body' },
                    a.createElement(
                      'div',
                      { className: 'previewer-msg-area' },
                      0 < (this.state.filesMissing || []).length &&
                        a.createElement(
                          'div',
                          { className: 'previewer-msg-group' },
                          a.createElement(
                            'h5',
                            null,
                            '\u5FFD\u7565\u4E0A\u4F20\u7684\u6587\u4EF6'
                          ),
                          a.createElement(
                            'p',
                            { style: { color: 'gray' } },
                            '\u4EE5\u4E0B\u6587\u4EF6\u6CA1\u6709\u4E0A\u4F20\u3002'
                          ),
                          a.createElement(
                            'ul',
                            null,
                            (this.state.filesMissing || []).map((b, c) => {
                              return a.createElement(
                                'p',
                                { key: c },
                                '\xB7 ' + b
                              )
                            })
                          )
                        ),
                      0 < (this.state.filesIgnored || []).length &&
                        a.createElement(
                          'div',
                          { className: 'previewer-msg-group' },
                          a.createElement(
                            'h5',
                            null,
                            '\u4F53\u79EF\u8FC7\u5927\u7684\u6587\u4EF6'
                          ),
                          a.createElement(
                            'p',
                            { style: { color: 'gray' } },
                            '\u4EE5\u4E0B\u6587\u4EF6\u4F53\u79EF\u8D85\u8FC7 500KB\uFF0C\u5DF2\u8DF3\u8FC7\u538B\u7F29\u4EE5\u53CA ES6 \u8F6C ES5 \u7684\u5904\u7406\u3002',
                            a.createElement(
                              'a',
                              {
                                style: { color: '#586c94' },
                                onClick: this.handleTipsLinkClick.bind(this)
                              },
                              '\u70B9\u51FB\u67E5\u770B\u8BE6\u60C5'
                            )
                          ),
                          a.createElement(
                            'ul',
                            null,
                            (this.state.filesIgnored || []).map((b, c) => {
                              return a.createElement(
                                'p',
                                { key: c },
                                '\xB7 ' + b
                              )
                            })
                          )
                        )
                    )
                  )
                )
              )
          ),
          a.createElement(
            'div',
            { className: 'preview-item' },
            a.createElement(
              'div',
              {
                className: 'ui-popover',
                style: {
                  position: 'relative',
                  width: '252px',
                  display: b.remoteDebug ? 'none' : ''
                },
                onClick: this.onHandleClick.bind(this)
              },
              a.createElement(
                'div',
                { className: 'previewer' },
                a.createElement(
                  'div',
                  {
                    className: 'previewer-header',
                    onClick: this.showPreviewTool.bind(this),
                    style: { cursor: 'pointer' }
                  },
                  a.createElement(
                    'h4',
                    null,
                    '\u5C0F\u7A0B\u5E8F\u5F00\u53D1\u52A9\u624B ',
                    a.createElement('i', {
                      className: 'ui-icon-ask',
                      style: { cursor: 'pointer' },
                      title: '\u70B9\u51FB\u67E5\u770B\u8BE6\u60C5',
                      onClick: this.handleIconAskClick.bind(this)
                    })
                  )
                ),
                a.createElement(
                  'div',
                  {
                    ref: a => (this.previewImgDom = a),
                    style: this.state.previewToolShown
                      ? {}
                      : { display: 'none' }
                  },
                  a.createElement(
                    'div',
                    { className: 'previewer-body' },
                    a.createElement(
                      'div',
                      { className: 'previewer-qrcode' },
                      a.createElement('img', {
                        onClick: this.onCopyClick.bind(this, 'preview'),
                        src: '../static/image/my-dev-apps.jpg',
                        alt: ''
                      })
                    )
                  )
                ),
                a.createElement(
                  'div',
                  {
                    className: 'preview-footer',
                    style: this.state.previewToolShown
                      ? {}
                      : { display: 'none' }
                  },
                  a.createElement(
                    'button',
                    {
                      onClick: this.onCopyClick.bind(this, 'preview'),
                      className: 'ui-button ui-button-default'
                    },
                    '\u590D\u5236\u4E8C\u7EF4\u7801'
                  )
                )
              )
            )
          )
        )
      )
    }
  }
  module.exports = r(
    (a, b) => {
      return {
        show:
          d.PREVIEWCODE === a.toolbar.clickKey ||
          d.REMOTEDEBUGCODE === a.toolbar.clickKey,
        appConfig: a.simulator.appConfig || {},
        project: a.project.current,
        nickName: (a.user || {}).nickName || '\u672A\u77E5\u7528\u6237',
        remoteDebug: 'remotedebug' === b.mode,
        remoteDebugWindow: a.window.remoteDebugWindow,
        compilerSettings: a.settings.compiler || {}
      }
    },
    a => {
      return {
        showError: j.bindActionCreators(f.showError, a),
        toggleClickKey: j.bindActionCreators(h.toggleClickKey, a),
        setPkgSize: j.bindActionCreators(i.setPkgSize, a),
        showSuccessTip: j.bindActionCreators(f.showSuccessTip, a),
        windowActions: j.bindActionCreators(m, a),
        consoleActions: j.bindActionCreators(g, a),
        updateIDESetting() {
          a(o.updateIDESetting.apply(this, arguments))
        }
      }
    }
  )(s)
})(require('lazyload'), require)
