"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _utils = require("../../_utils/utils");
var _locale = require("../../../src/locale");
require("./index.less");
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 CopyTitle = function CopyTitle(props) {
  var title = props.title,
    getExportStrData = props.getExportStrData,
    getImportStrData = props.getImportStrData;
  var _useState = (0, _react.useState)(false),
    importDialogVisible = _useState[0],
    setImportDialogVisible = _useState[1];
  var _useState2 = (0, _react.useState)(""),
    importConditionsStr = _useState2[0],
    setImportConditionsStr = _useState2[1];

  /**
   * 导出关系配置信息
   */
  function exportComputed() {
    return _exportComputed.apply(this, arguments);
  }
  /**
   * 导入关系配置信息
   */
  function _exportComputed() {
    _exportComputed = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var copyData, copyStr;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            copyData = getExportStrData();
            if (copyData !== null) {
              copyStr = JSON.stringify(copyData, null, "\t");
              (0, _utils.copyToClipboard)(copyStr);
              _message["default"].success((0, _locale.t)("setter.configTip"));
            }
          case 2:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return _exportComputed.apply(this, arguments);
  }
  function importComputed() {
    if (importConditionsStr.length) {
      try {
        var res = JSON.parse(importConditionsStr);
        getImportStrData(res);
        closeImportDialog();
      } catch (error) {
        _message["default"].error((0, _locale.t)("setter.importErrorTip"));
      }
    } else {
      _message["default"].error((0, _locale.t)("setter.inputPlaceHolder"));
    }
  }
  function closeImportDialog() {
    setImportDialogVisible(false);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "ComputedTitle"
  }, title && /*#__PURE__*/_react["default"].createElement("span", null, title), /*#__PURE__*/_react["default"].createElement("div", {
    className: "ComputedTitleBtn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    type: "primary",
    style: {
      marginRight: 10
    },
    onClick: exportComputed
  }, (0, _locale.t)("setter.exportConfig")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    type: "primary",
    onClick: function onClick() {
      setImportDialogVisible(true);
    }
  }, (0, _locale.t)("setter.importConfig"))), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "CComputed-dialog",
    visible: importDialogVisible,
    title: (0, _locale.t)("setter.importContext"),
    footerAlign: "right",
    onClose: closeImportDialog,
    onOk: importComputed,
    footer: [/*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: importComputed,
      type: "primary"
    }, (0, _locale.t)('setter.ok')), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        marginLeft: '10px'
      },
      onClick: closeImportDialog
    }, (0, _locale.t)('setter.cancel'))]
  }, /*#__PURE__*/_react["default"].createElement(_input["default"].TextArea, {
    autoHeight: {
      minRows: 20
    },
    style: {
      width: "100%"
    },
    placeholder: "" + (0, _locale.t)("setter.inputPlaceHolder") + (0, _locale.t)("setter.importContext"),
    value: importConditionsStr,
    onChange: function onChange(e) {
      setImportConditionsStr(e);
    }
  })));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CopyTitle, this.props);
  };
  return _default;
}(_react.Component);