'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isNum = _interopDefault(require('./isNum'));
var isStr = _interopDefault(require('./isStr'));
var consts = require('./consts');

/**
 * 设置Cookie数据
 * @param {string} key
 * @param {string} value
 * @param {{ exMinuts?: number; exDays?: number; domain?: string; encode?: boolean}} options
 *
 * setCookie('cookie1', 'hello cookie');
 * // 设置bilibili.com域下的cookie
 * setCookie('cookie2', 'hello cookie', { domain: '.bilibili.com' });
 */
function setCookie(key, value, options) {
  var expires = '',
      exTime = 0;
  options = options || {};

  if (options.encode !== false) {
    value = encodeURIComponent(value);
  }

  if (isNum(options.exMinuts)) {
    exTime += options.exMinuts * 6e4;
  }

  if (isNum(options.exDays)) {
    exTime += options.exDays * consts.DAY_TIME;
  }

  if (exTime) {
    var date = new Date();
    date.setTime(date.getTime() + exTime);
    expires = ";expires=".concat(date.toUTCString());
  }

  var domain = isStr(options.domain) ? ";domain=".concat(options.domain) : '';
  document.cookie = "".concat(key, "=").concat(value).concat(expires, ";path=/").concat(domain);
}

module.exports = setCookie;
