/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { pkg } from "../../settings.js";
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";

//#region src/components/APIKeyModal/APIKeyDownloader.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const componentName = "APIKeyDownloader";
const APIKeyDownloader = (props) => {
	const { apiKey, body, fileName, fileType, linkText, downloadLinkLabel } = props;
	const [linkProps, setLinkProps] = useState({});
	useEffect(() => {
		const generateLinkProps = async () => {
			const data = fileType === "txt" ? apiKey : JSON.stringify({ apiKey });
			const blob = new Blob([data], { type: fileType === "txt" ? "text/plain" : "application/json" });
			setLinkProps({
				href: await URL.createObjectURL(blob),
				download: `${fileName || "apikey"}.${fileType}`
			});
		};
		generateLinkProps();
	}, [
		apiKey,
		fileName,
		fileType
	]);
	return /* @__PURE__ */ React.createElement("div", { className: `${pkg.prefix}--apikey-modal__download-container` }, /* @__PURE__ */ React.createElement("p", { className: `${pkg.prefix}--apikey-modal__messaging-text` }, body, " ", /* @__PURE__ */ React.createElement("a", {
		...linkProps,
		className: `${pkg.prefix}--apikey-modal__download-link`,
		"aria-label": downloadLinkLabel ?? linkText,
		role: "button"
	}, downloadLinkLabel ?? linkText)));
};
APIKeyDownloader.displayName = componentName;
APIKeyDownloader.propTypes = {
	apiKey: PropTypes.string.isRequired,
	body: PropTypes.string,
	downloadLinkLabel: PropTypes.string,
	fileName: PropTypes.string.isRequired,
	fileType: PropTypes.oneOf(["txt", "json"]).isRequired,
	linkText: PropTypes.string.isRequired
};

//#endregion
export { APIKeyDownloader };