(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';

var _message = require('../page/message');

var _retailerMode = require('../msgConst/retailerMode');

var _dom = require('../common/dom');

var _domStyle = require('../common/dom-style');

let btnContainer = 'nexus-QN-injection',
    injectionSelector = '[class^=buyer-mod__buyer]',
    navSelector = 'pagination',
    closeBtnClass = 'uc-nexus-retailer-close-qn-injection',
    QN_LINK = 'https://www.taobao.com/market/work/portal/pc.php?from=uc',
    watchInterval;

(0, _message.sendMessageAsync)(_retailerMode.MSG_EVENT_SHOULD_INJECT_QN_BTN, null).then(function (_ref) {
  let disabled = _ref.disabled;
  let installed = _ref.installed;

  if (!disabled && !installed) {
    injectBtns();
    document.body.addEventListener('click', function (e) {
      if (e.path.some(ele => ele.classList && ele.classList.contains(navSelector))) {
        watchInterval = setInterval(() => {
          if (shouldInsert()) {
            injectBtns();
            clearInterval(watchInterval);
          }
        }, 1000);
      } else if (e.target.classList.contains(closeBtnClass)) {
        (0, _message.sendMessageAsync)(_retailerMode.MSG_EVENT_DISABLE_QN_INJECTION, {}, { noReply: true });
        dispose();
        e.preventDefault();
      }
    });
  }
});

function shouldInsert() {
  return !(0, _dom.$)(`${ injectionSelector } .${ btnContainer }`);
}

function injectBtns() {
  (0, _dom.$$)(injectionSelector).forEach(container => {
    let btn = createInjectionBtn();
    container.appendChild(btn);
  });
}
function dispose() {
  removeAll();
  clearTimeout(watchInterval);
}
(0, _message.registerMsgHandler)(_retailerMode.MSG_EVENT_DISABLE_QN_INJECTION, dispose);
function removeAll() {
  (0, _dom.$$)('.' + btnContainer).forEach(_dom.removeNodeFromParent);
}

function createInjectionBtn() {
  let p = document.createElement('p');
  p.classList.add(btnContainer);
  p.innerHTML = `<a href="${ QN_LINK }" target="_blank" style="${ containerStyle }" >
            <span class="uc-nexus-retailer-mode-icon-download-qn" style="${ iconStyle }"></span>
            <span>${ chrome.i18n.getMessage('retailer_download_QN') }</span>
            <span class="${ closeBtnClass } close"></span>
        </a>`;
  return p;
}
let iconStyle = (0, _domStyle.flatStyle)({
  display: 'inline-block',
  marginRight: '4px',
  marginTop: '-1px',
  verticalAlign: 'top'
}),
    containerStyle = (0, _domStyle.flatStyle)({
  color: '#3eac13',
  lineHeight: '17px'
});

},{"../common/dom":4,"../common/dom-style":3,"../msgConst/retailerMode":6,"../page/message":7}],2:[function(require,module,exports){
"use strict";

exports.defer = defer;
function defer() {
  let resolve,
      reject,
      promise = new Promise((_res, _rej) => (resolve = _res, reject = _rej));
  return { resolve, reject, promise };
}

},{}],3:[function(require,module,exports){
'use strict';

exports.flatStyle = flatStyle;

var _utils = require('./utils');

function flatStyle(styleObject) {
  let rules = [];
  (0, _utils.forEach)(styleObject, (val, key) => rules.push(`${ key.replace(/([A-Z])/g, c => '-' + c.toLowerCase()) }:${ val }`));
  return rules.join(';');
}

},{"./utils":5}],4:[function(require,module,exports){
'use strict';

exports.$ = $;
exports._$ = _$;
exports._$$ = _$$;
exports.$$ = $$;
exports.toggleClass = toggleClass;
exports.removeNodeFromParent = removeNodeFromParent;
let slice = Array.prototype.slice;
function $(slt, ele) {
  if (slt instanceof HTMLElement) {
    return slt;
  }
  return (ele || document).querySelector(slt);
}
function _$(slt, ele) {
  return (ele || document).shadowRoot.querySelector(slt);
}
function _$$(slt, ele) {
  return slice.call((ele || document).shadowRoot.querySelectorAll(slt));
}
function $$(slt, ele) {
  return slice.call((ele || document).querySelectorAll(slt));
}
function toggleClass(ele, className, showClass) {
  let hasClass = arguments.length == 2 ? ele.classList.contains(className) : !showClass;
  ele.classList[hasClass ? 'remove' : 'add'](className);
}
function removeNodeFromParent(node) {
  if (node && node.parentNode) {
    node.parentNode.removeChild(node);
    return true;
  }
  return false;
}

},{}],5:[function(require,module,exports){
'use strict';

exports.forEach = forEach;
exports.arrAdd = arrAdd;
exports.arrFind = arrFind;
exports.arrFirst = arrFirst;
exports.arrRemove = arrRemove;
exports.arrFilterMappedResult = arrFilterMappedResult;
exports.isFunc = isFunc;
exports.isObj = isObj;
exports.noop = noop;
function forEach(object, callback, thisObj) {
  if (isObj(object)) {
    if (thisObj === void 0) {
      thisObj = object;
    }
    if (object instanceof Array || object.hasOwnProperty('length')) {
      for (let i = 0, len = object.length; i < len; i++) {
        callback.call(thisObj, object[i], i);
      }
    } else {
      for (let name in object) {
        if (object.hasOwnProperty(name)) {
          callback.call(thisObj, object[name], name);
        }
      }
    }
  }
  return object;
}
function arrAdd(array, item, first) {
  if (array.indexOf(item) == -1) {
    array[first ? 'unshift' : 'push'](item);
    return true;
  }
}
function arrFind(arr, test) {
  let index = 0;
  for (let item of arr) {
    if (test(item, index++)) {
      return item;
    }
  }
}
function arrFirst(arr, map) {
  let index = 0;
  for (let item of arr) {
    let val = map(item, index++);
    if (val) {
      return val;
    }
  }
}
function arrRemove(arr, item) {
  let index = arr.indexOf(item);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return index != -1;
}
function arrFilterMappedResult(arr, mapFunc, thisObj) {
  let ret = [],
      item;
  forEach(arr, function (val, key) {
    if (item = mapFunc.call(this, val, key)) {
      ret.push(item);
    }
  }, thisObj);
  return ret;
}
function isFunc(any) {
  return typeof any === 'function';
}
function isObj(any) {
  return any && typeof any === 'object';
}
function noop() {}

},{}],6:[function(require,module,exports){
'use strict';

const MSG_EVENT_SHOULD_INJECT_QN_BTN = exports.MSG_EVENT_SHOULD_INJECT_QN_BTN = 'inject-QNBTN?',
      MSG_EVENT_DISABLE_QN_INJECTION = exports.MSG_EVENT_DISABLE_QN_INJECTION = 'disable-QNBTN',
      STORAGE_KEY_USER_DISABLE_QN_INJECTION = exports.STORAGE_KEY_USER_DISABLE_QN_INJECTION = 'QN_Injection_Disabled';

},{}],7:[function(require,module,exports){
'use strict';

exports.sendMessageAsync = sendMessageAsync;
exports.registerMsgHandler = registerMsgHandler;

var _defer = require('../common/defer');

let deferCache,
    bkMsgHandlers = Object.create(null),
    extPort;
if (typeof chrome == "object" && chrome.runtime && chrome.runtime.onMessage) {
  chrome.runtime.onMessage.addListener(onMsg);
}
window.addEventListener('unload', () => {
  extPort.disconnect();
  disposePort();
});
function sendMessageAsync(type, data) {
  var _ref = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

  let noReply = _ref.noReply;
  let extId = _ref.extId;

  let msg = { type, data, sync: false, noReply, d: 'req' };

  if (noReply) {
    sendMsg(extId, msg);
    return Promise.resolve();
  } else {
    let uid = guid(),
        defered = (0, _defer.defer)();
    msg.guid = uid;
    deferCache[uid] = defered;
    sendMsg(extId, msg);
    return defered.promise;
  }
}
initPort();
function initPort() {
  deferCache = Object.create(null);
  try {
    extPort = chrome.runtime.connect({ name: 'page:connect' });
    extPort.onMessage.addListener(onMsg);
    extPort.onDisconnect.addListener(function () {
      disposePort();
      setTimeout(initPort, 0);
    });
  } catch (ex) {
    console.log(ex);
  }
}
function disposePort() {
  for (let defer in deferCache) {
    defer.reject('disconnect');
  }
  extPort = null;
}
function onMsg(response) {
  let guid = response.guid;
  let d = response.d;let defer = deferCache[guid];
  if (d === 'postTab') {
    let handler = bkMsgHandlers[response.type];
    handler && handler(response.data);
  } else if (defer) {
    if (d === 'res') {
      response.success ? defer.resolve(response.data) : defer.reject(response.error);
      delete deferCache[guid];
    }
  } else {
    console.warn('response with:' + guid + ' not found');
  }
}
function registerMsgHandler(event, handler) {
  if (typeof handler === "function") {
    bkMsgHandlers[event] = handler;
  }
}
function sendMsg(extId, msg) {
  if (extId && msg) {
    throw Error('not support external');
  } else if (msg) {
    if (extPort) {
      extPort.postMessage(msg);
    } else {
      chrome.runtime.sendMessage(msg);
    }
  }
}

function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  }

  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + Date.now();
}

},{"../common/defer":2}]},{},[1]);
