"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkFileName = exports.calcLastTime = exports.addZero = void 0;
exports.commonFetch = commonFetch;
exports.convertImageUrl = convertImageUrl;
exports.dragSource = void 0;
exports.ensure = ensure;
exports.styleStringify = exports.navigateTo = exports.getUrlParams = exports.getColorRgba = void 0;

var _axios = _interopRequireDefault(require("axios"));

var _qs = _interopRequireDefault(require("qs"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

/**
 * 安全获取数据
 * @param {function} func 获取函数
 * @param {any} defaults 默认值
 */
function ensure(func, defaults) {
  try {
    return func() || defaults;
  } catch (error) {
    return defaults;
  }
}
/**
 * fish fetch
 * @param {*} url 请求地址
 * @param {*} data 入参
 * @param {*} method 请求方法
 */


function commonFetch(url, data, method) {
  if (data === void 0) {
    data = {};
  }

  if (method === void 0) {
    method = 'GET';
  }

  var _method = method.toUpperCase();

  return new Promise(function (resolve) {
    var _a, _b;

    var isLoginUrl = url.includes('portal/login');
    var headers = {
      'Content-Type': isLoginUrl ? 'application/x-www-form-urlencoded' : 'application/json; charset=UTF-8'
    };

    if (_method === 'GET') {
      if ((_a = window.magicDesign) === null || _a === void 0 ? void 0 : _a.fetch) {
        window.magicDesign.fetch(url, {
          method: 'GET',
          params: data
        }).then(function (res) {
          resolve(res);
        });
        return;
      }

      _axios["default"].get(url, {
        params: data,
        headers: headers
      }).then(function (res) {
        resolve(res.data);
      });
    }

    if (_method === 'POST') {
      if ((_b = window.magicDesign) === null || _b === void 0 ? void 0 : _b.fetch) {
        window.magicDesign.fetch(url, {
          method: 'POST',
          data: data
        }).then(function (res) {
          resolve(res);
        });
        return;
      }

      _axios["default"].post(url, isLoginUrl ? _qs["default"].stringify(data) : data, {
        headers: headers
      }).then(function (res) {
        resolve(res.data);
      });
    }
  });
}

var navigateTo = function navigateTo(url) {
  if (!window.magicDesign || !window.magicDesign.history) {
    window.location.href = url;
    return;
  }

  window.magicDesign.history.push(url);
};

exports.navigateTo = navigateTo;

var dragSource = function dragSource(source, from, to) {
  var out = source.splice(from, 1);

  if (from > to) {
    source.splice(to, 0, out[0]);
  } else {
    source.splice(to, 0, out[0]);
  }

  return source;
}; // 校验文件名称,不能存在 / \ ,


exports.dragSource = dragSource;

var checkFileName = function checkFileName(fileName) {
  if (fileName === undefined || fileName === null || fileName === '') {
    return false;
  }

  var reg = /[\\,/]+/;

  if (reg.test(fileName)) {
    return false;
  }

  return true;
}; // 获取浏览器参数


exports.checkFileName = checkFileName;

var getUrlParams = function getUrlParams(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", 'i'); // 定义正则表达式

  var r = ensure(function () {
    return window.location.href.split('?')[1].match(reg);
  }, null);
  if (r != null) return unescape(r[2]);
  return null;
};
/**
 * 补0 函数
 * @param {*} num 数字
 */


exports.getUrlParams = getUrlParams;

var addZero = function addZero(num) {
  return String(num).length === 1 ? "0" + String(num) : String(num);
};
/**
 * 计算倒计时
 * @param {number} lastTime 剩余时间 单位：毫秒
 * @param {string?} type 'day' 以日为单位
 * @returns
 */


exports.addZero = addZero;

var calcLastTime = function calcLastTime(lastTime, type) {
  if (lastTime <= 0) {
    return 0;
  }

  var day = ~~(lastTime / 1000 / 60 / 60 / 24);
  var hours = ~~(lastTime / 1000 / 60 / 60);
  var mines = ~~(lastTime / 1000 / 60 % 60);
  var second = ~~(lastTime / 1000 % 60);

  if (type === 'day') {
    hours = ~~(lastTime / 1000 / 60 / 60 % 24);
    return day + "DAYS  " + addZero(hours) + ":" + addZero(mines) + ":" + addZero(second);
  }

  if (type === '_hour') {
    return hours;
  }

  if (type === '_mines') {
    return mines;
  }

  return addZero(hours) + ":" + addZero(mines) + ":" + addZero(second);
};
/**
 * 获取图片URL
 * @param {object} imageObj 图片对象
 * @param {string?} type small | big
 */


exports.calcLastTime = calcLastTime;

function convertImageUrl(imageObj, type) {
  if (imageObj === void 0) {
    imageObj = {};
  }

  if (!imageObj) return '';
  var typeMap = {
    small: 'smallImgUrl',
    big: 'bigImgUrl'
  }[type];
  return ensure(function () {
    return encodeURI(imageObj[typeMap] || imageObj.url);
  }, '');
}
/** 序列化 style对象 */


var styleStringify = function styleStringify(style, _a) {
  var _b = (_a === void 0 ? {} : _a).suffix,
      suffix = _b === void 0 ? 'px' : _b;
  var numericProperties = ['height', 'width', 'top', 'left', 'right', 'bottom', 'fontSize', 'maxHeight', 'maxWidth', 'lineHeight', 'margin', 'marginTop', 'marginLeft', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom'];
  return Object.keys(style).reduce(function (styleStr, key) {
    var value = style[key];
    if (value == null) return styleStr;
    var needSuffix = numericProperties.includes(key) && typeof value === 'number';
    var newName = key.replace(/([A-Z])/g, '-$1').toLowerCase();
    return "" + styleStr + newName + ": " + value + (needSuffix ? suffix : '') + "; ";
  }, '');
};
/** 十六进制色值转换成rgba */


exports.styleStringify = styleStringify;

var getColorRgba = function getColorRgba(sHex, alpha) {
  if (alpha === void 0) {
    alpha = 1;
  }

  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  var sColor = sHex.toLowerCase();

  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = '#';

      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
      }

      sColor = sColorNew;
    }

    var sColorChange = [];

    for (var i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)));
    }

    return 'rgba(' + sColorChange.join(',') + ',' + alpha + ')';
  } else {
    return sColor;
  }
};

exports.getColorRgba = getColorRgba;