import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useContext, useEffect, useMemo, useState } from 'react';
import ConfigContext from '../../../es/config-provider/ConfigContext';
import isString from 'lodash/isString';
import isFunction from 'lodash/isFunction';
import isPromise from 'is-promise';
import { ButtonColor } from '../button/enum';
import Button from '../button';
import { $l } from '../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 ? ButtonColor.primary : _props$color;

  var _useContext = useContext(ConfigContext),
      getConfig = _useContext.getConfig;

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

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

  var _useState = useState(function () {
    return downloadUrl;
  }),
      _useState2 = _slicedToArray(_useState, 2),
      templateDownloadUrl = _useState2[0],
      setTemplateDownloadUrl = _useState2[1];

  useEffect(function () {
    if (isPromise(downloadUrl)) {
      downloadUrl.then(function (url) {
        return setTemplateDownloadUrl(function () {
          return url;
        });
      });
    } else {
      setTemplateDownloadUrl(function () {
        return downloadUrl;
      });
    }
  }, [downloadUrl]);

  if (templateDownloadUrl === undefined || isPromise(templateDownloadUrl)) {
    return null;
  }

  var downProps = {
    funcType: funcType,
    href: isString(templateDownloadUrl) ? templateDownloadUrl : undefined,
    onClick: isFunction(templateDownloadUrl) ? templateDownloadUrl : undefined,
    target: target,
    color: color,
    style: templateStyle
  };
  return /*#__PURE__*/React.createElement(Button, _extends({}, downProps), "(", $l('Attachment', 'download_template'), ")");
};

TemplateDownloadButton.displayName = 'TemplateDownloadButton';
export default TemplateDownloadButton;
//# sourceMappingURL=TemplateDownloadButton.js.map
