import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Dialog from "@alifd/next/es/dialog";
import _Input from "@alifd/next/es/input";
import _Button from "@alifd/next/es/button";
import _Message from "@alifd/next/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { Component, useState } from "react";
import { copyToClipboard } from "../../_utils/utils";
import { t } from "../../../src/locale";
import "./index.less";
var CopyTitle = function CopyTitle(props) {
  var title = props.title,
    getExportStrData = props.getExportStrData,
    getImportStrData = props.getImportStrData;
  var _useState = useState(false),
    importDialogVisible = _useState[0],
    setImportDialogVisible = _useState[1];
  var _useState2 = useState(""),
    importConditionsStr = _useState2[0],
    setImportConditionsStr = _useState2[1];

  /**
   * 导出关系配置信息
   */
  function exportComputed() {
    return _exportComputed.apply(this, arguments);
  }
  /**
   * 导入关系配置信息
   */
  function _exportComputed() {
    _exportComputed = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var copyData, copyStr;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            copyData = getExportStrData();
            if (copyData !== null) {
              copyStr = JSON.stringify(copyData, null, "\t");
              copyToClipboard(copyStr);
              _Message.success(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.error(t("setter.importErrorTip"));
      }
    } else {
      _Message.error(t("setter.inputPlaceHolder"));
    }
  }
  function closeImportDialog() {
    setImportDialogVisible(false);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "ComputedTitle"
  }, title && /*#__PURE__*/React.createElement("span", null, title), /*#__PURE__*/React.createElement("div", {
    className: "ComputedTitleBtn"
  }, /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    style: {
      marginRight: 10
    },
    onClick: exportComputed
  }, t("setter.exportConfig")), /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    onClick: function onClick() {
      setImportDialogVisible(true);
    }
  }, t("setter.importConfig"))), /*#__PURE__*/React.createElement(_Dialog, {
    className: "CComputed-dialog",
    visible: importDialogVisible,
    title: t("setter.importContext"),
    footerAlign: "right",
    onClose: closeImportDialog,
    onOk: importComputed,
    footer: [/*#__PURE__*/React.createElement(_Button, {
      onClick: importComputed,
      type: "primary"
    }, t('setter.ok')), /*#__PURE__*/React.createElement(_Button, {
      style: {
        marginLeft: '10px'
      },
      onClick: closeImportDialog
    }, t('setter.cancel'))]
  }, /*#__PURE__*/React.createElement(_Input.TextArea, {
    autoHeight: {
      minRows: 20
    },
    style: {
      width: "100%"
    },
    placeholder: "" + t("setter.inputPlaceHolder") + t("setter.importContext"),
    value: importConditionsStr,
    onChange: function onChange(e) {
      setImportConditionsStr(e);
    }
  })));
};
var _default = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(CopyTitle, this.props);
  };
  return _default;
}(Component);
export { _default as default };