"use strict";

exports.__esModule = true;
exports.useDownloadFile = void 0;
var _react = require("@wix/bex-core/react");
var _react2 = require("react");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const useDownloadFile = ({
  file
}) => {
  const {
    httpClient,
    window,
    errorHandler: {
      withErrorHandler,
      showError
    },
    translate: t
  } = (0, _react.useWixPatternsContainer)();
  const [isDownloading, setIsDownloading] = (0, _react2.useState)(false);
  const downloadFile = async () => {
    try {
      var _data$downloadUrls;
      setIsDownloading(true);
      const {
        generateFileDownloadUrl
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('@wix/bex-utils/@wix/ambassador-media-site-media-v1-file-descriptor/http')));
      const {
        data
      } = await withErrorHandler(() => httpClient.request(generateFileDownloadUrl({
        fileId: file.mediaId
      })), {
        INVALID_ASSET_KEY: false
      });
      const downloadableUrl = (_data$downloadUrls = data.downloadUrls) == null || (_data$downloadUrls = _data$downloadUrls[0]) == null ? void 0 : _data$downloadUrls.url;
      if (!downloadableUrl) {
        throw new Error(t('cairo.downloadable-url.error.generic'));
      }
      window.download(downloadableUrl, file.displayName);
    } catch (error) {
      showError == null || showError(error);
    } finally {
      setIsDownloading(false);
    }
  };
  return {
    downloadFile,
    isDownloading
  };
};
exports.useDownloadFile = useDownloadFile;
//# sourceMappingURL=useDownloadFile.js.map