"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setMagicDesign = exports.renderPreview = exports.isPc = exports.isDesignMode = exports.copyNode = exports.clickUrl = void 0;

var _uuid = require("uuid");

var _businessUtil = require("./businessUtil");

var _commonUtil = require("./commonUtil");

var _androidUtil = require("./androidUtil");

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var _this = void 0;

var isDesignMode = function isDesignMode() {
  return window.magicDesign.mode === 'designer';
};

exports.isDesignMode = isDesignMode;

var isPc = function isPc() {
  return window.magicDesign.device === 'pc';
};

exports.isPc = isPc;

var setMagicDesign = function setMagicDesign(magicDesignObj) {
  window.magicDesign = __assign(__assign({}, window.magicDesign), magicDesignObj);
};
/** 刷新预览区域 */


exports.setMagicDesign = setMagicDesign;

var renderPreview = function renderPreview() {
  var evt = new Event('render-view', {
    bubbles: false
  });
  window.document.dispatchEvent(evt);
};

exports.renderPreview = renderPreview;

var clickUrl = function clickUrl(url) {
  console.log('url', url);

  if (window.magicDesign.readOnly || window.magicDesign.mode !== 'renderer' || !url) {
    return;
  }

  var _a = (0, _businessUtil.parseUrl)(url),
      h5Link = _a.h5Link,
      pcLink = _a.pcLink,
      params = _a.params;

  var type = params.type,
      data = params.data; // 领取优惠券

  if (type === 'GET_COUPON') {
    _this.getCoupon(data.couponId);

    return;
  }

  if (!(0, _androidUtil.browserVersion)().isApp) {
    if (url.startsWith('/')) {
      (0, _commonUtil.navigateTo)(url);
    } else {
      location.href = url;
    }

    return;
  }

  if ((0, _androidUtil.browserVersion)().android) {
    sendToAndroid({
      type: 'jump',
      params: {
        url: h5Link
      }
    });
  } else if ((0, _androidUtil.browserVersion)().ios) {
    if (type === 'CUSTOMIZED') {
      // 适配ios 从首页跳转自定义页面
      _this.goH5Page(h5Link);
    }

    sendToIos(params);
  }
};

exports.clickUrl = clickUrl;

function isObject(value) {
  var type = _typeof(value);

  return value != null && (type === 'object' || type === 'function');
} // 拷贝节点方法，重新生成节点中的id和parentId字段


var copyNode = function copyNode(value, parentId) {
  if (!isObject(value)) {
    return value;
  }

  var isArr = Array.isArray(value);

  if (isArr) {
    var newArray_1 = [];
    value.forEach(function (data) {
      newArray_1.push(copyNode(data));
    });
    return newArray_1;
  }

  var newNode = {};

  if (value.id) {
    newNode.id = (0, _uuid.v4)();
  }

  var names = Object.getOwnPropertyNames(value);
  names.forEach(function (name) {
    if (name === 'id') return;

    if (name === 'parentId') {
      newNode.parentId = parentId;
    } else {
      newNode[name] = copyNode(value[name], newNode.id);
    }
  });
  return newNode;
};

exports.copyNode = copyNode;