"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireWildcard(require("react"));

var _ConfigContext = _interopRequireDefault(require("../../../lib/config-provider/ConfigContext"));

var _isString = _interopRequireDefault(require("lodash/isString"));

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

var _isPromise = _interopRequireDefault(require("is-promise"));

var _enum = require("../button/enum");

var _button = _interopRequireDefault(require("../button"));

var _localeContext = require("../locale-context");

var templateStyle = {
  marginLeft: 0
};

var TemplateDownloadButton = function TempalteDownloadButton(props) {
  var attachmentUUID = props.attachmentUUID,
      bucketName = props.bucketName,
      bucketDirectory = props.bucketDirectory,
      storageCode = props.storageCode,
      isPublic = props.isPublic,
      target = props.target,
      funcType = props.funcType,
      _props$color = props.color,
      color = _props$color === void 0 ? _enum.ButtonColor.primary : _props$color;

  var _useContext = (0, _react.useContext)(_ConfigContext["default"]),
      getConfig = _useContext.getConfig;

  var _getConfig = getConfig('attachment'),
      getTemplateDownloadUrl = _getConfig.getTemplateDownloadUrl;

  var downloadUrl = (0, _react.useMemo)(function () {
    return getTemplateDownloadUrl ? getTemplateDownloadUrl({
      attachmentUUID: attachmentUUID,
      bucketName: bucketName,
      bucketDirectory: bucketDirectory,
      storageCode: storageCode,
      isPublic: isPublic
    }) : attachmentUUID;
  }, [getTemplateDownloadUrl, attachmentUUID, bucketName, bucketDirectory, storageCode, isPublic]);

  var _useState = (0, _react.useState)(function () {
    return downloadUrl;
  }),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      templateDownloadUrl = _useState2[0],
      setTemplateDownloadUrl = _useState2[1];

  (0, _react.useEffect)(function () {
    if ((0, _isPromise["default"])(downloadUrl)) {
      downloadUrl.then(function (url) {
        return setTemplateDownloadUrl(function () {
          return url;
        });
      });
    } else {
      setTemplateDownloadUrl(function () {
        return downloadUrl;
      });
    }
  }, [downloadUrl]);

  if (templateDownloadUrl === undefined || (0, _isPromise["default"])(templateDownloadUrl)) {
    return null;
  }

  var downProps = {
    funcType: funcType,
    href: (0, _isString["default"])(templateDownloadUrl) ? templateDownloadUrl : undefined,
    onClick: (0, _isFunction["default"])(templateDownloadUrl) ? templateDownloadUrl : undefined,
    target: target,
    color: color,
    style: templateStyle
  };
  return /*#__PURE__*/_react["default"].createElement(_button["default"], (0, _extends2["default"])({}, downProps), "(", (0, _localeContext.$l)('Attachment', 'download_template'), ")");
};

TemplateDownloadButton.displayName = 'TemplateDownloadButton';
var _default = TemplateDownloadButton;
exports["default"] = _default;
//# sourceMappingURL=TemplateDownloadButton.js.map
