"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.linkBehaviorAction = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var React = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var fetchLinkList = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(url) {
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!url) {
            _context.next = 6;
            break;
          }
          _context.next = 3;
          return fetch(url);
        case 3:
          _context.t0 = _context.sent.json();
          _context.next = 7;
          break;
        case 6:
          _context.t0 = [];
        case 7:
          return _context.abrupt("return", _context.t0);
        case 8:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function fetchLinkList(_x) {
    return _ref.apply(this, arguments);
  };
}();
function fillDefaultValue(value) {
  if (typeof value !== 'object') {
    console.warn('value passed to fillDefaultValue should be an object');
    return;
  }
  if (!value.target) {
    value.target = '_self';
  }
  if (!value.type) {
    value.type = 'internal';
  }
}
var LinkContent = function LinkContent(_ref2) {
  var _ref2$value = _ref2.value,
    value = _ref2$value === void 0 ? {} : _ref2$value,
    _onChange = _ref2.onChange,
    _ref2$options = _ref2.options,
    options = _ref2$options === void 0 ? {} : _ref2$options;
  fillDefaultValue(value);
  var url = options.url,
    responseFormatter = options.responseFormatter;
  var formatter = responseFormatter;
  var _React$useState = React.useState([]),
    data = _React$useState[0],
    setData = _React$useState[1];
  React.useEffect(function () {
    var ignore = false;
    fetchLinkList(url).then(function (result) {
      if (!ignore) {
        setData(formatter ? formatter(result) : result);
      }
    });
    return function () {
      ignore = true;
    };
  }, [url, formatter]);
  return /*#__PURE__*/React.createElement(_box["default"], null, /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u8DF3\u8F6C\u65B9\u5F0F"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, /*#__PURE__*/React.createElement(_radio["default"].Group, {
    size: "small",
    dataSource: [{
      label: '当前窗口打开',
      value: '_self'
    }, {
      label: '新窗口打开',
      value: '_blank'
    }],
    defaultValue: "_self",
    shape: "button",
    value: value.target || '_self',
    onChange: function onChange(target) {
      _onChange((0, _extends2["default"])({}, value, {
        target: target
      }));
    }
  }))), /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u8DF3\u8F6C\u7C7B\u578B"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, /*#__PURE__*/React.createElement(_radio["default"].Group, {
    size: "small",
    dataSource: [{
      label: '内部页面',
      value: 'internal'
    }, {
      label: '外部链接',
      value: 'external'
    }],
    defaultValue: "internal",
    shape: "button",
    value: value.type || 'internal',
    onChange: function onChange(type) {
      _onChange((0, _extends2["default"])({}, value, {
        type: type
      }));
    }
  }))), /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u8DF3\u8F6C\u9875\u9762"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, (value === null || value === void 0 ? void 0 : value.type) === 'external' ? /*#__PURE__*/React.createElement(_input["default"], {
    size: "small",
    hasClear: true,
    placeholder: "\u8BF7\u8F93\u5165\u94FE\u63A5",
    value: value.url,
    onChange: function onChange(val) {
      _onChange({
        type: value.type || 'internal',
        url: val
      });
    }
  }) : /*#__PURE__*/React.createElement(_select["default"], {
    size: "small",
    hasClear: true,
    showSearch: true,
    dataSource: data,
    value: value === null || value === void 0 ? void 0 : value.url,
    onChange: function onChange(val) {
      _onChange((0, _extends2["default"])({}, value, {
        url: val
      }));
    }
  }))));
};
var linkBehaviorAction = exports.linkBehaviorAction = {
  name: 'link',
  title: '链接',
  render: function render(props) {
    return /*#__PURE__*/React.createElement(LinkContent, props);
  },
  toActionValue: function toActionValue(link) {
    return link.url ? {
      type: 'JSExpression',
      value: "function() {window.open('" + link.url + "', '" + link.target + "');}"
    } : null;
  }
};