var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
import { Input } from "@theme-ui/components";
import { useState } from "react";
import { Flex, Text } from "@theme-ui/components";
import { Popup } from "../components/popup";
import { downloadImage, toDataURL } from "../../utils/downloader";
import { useToolbarStore } from "../stores/toolbar-store";
export function ImageUploadPopup(props) {
    const { onInsert, onClose } = props;
    const [isDownloading, setIsDownloading] = useState(false);
    const [error, setError] = useState();
    const [url, setUrl] = useState("");
    const downloadOptions = useToolbarStore((store) => store.downloadOptions);
    return (_jsx(Popup, { title: "Insert image from URL", onClose: onClose, action: {
            loading: isDownloading,
            title: "Insert image",
            disabled: !url,
            onClick: () => __awaiter(this, void 0, void 0, function* () {
                setIsDownloading(true);
                setError(undefined);
                try {
                    const image = yield downloadImage(url, downloadOptions);
                    if (!image)
                        return;
                    const { blob, size, mimeType } = image;
                    onInsert({ src: yield toDataURL(blob), size, mime: mimeType });
                }
                catch (e) {
                    if (e instanceof Error)
                        setError(e.message);
                }
                finally {
                    setIsDownloading(false);
                }
            })
        }, children: _jsxs(Flex, { sx: { px: 1, flexDirection: "column", width: ["auto", 350] }, children: [_jsx(Input, { type: "url", autoFocus: true, placeholder: "Paste Image URL here", value: url, onChange: (e) => {
                        setUrl(e.target.value);
                        setError(undefined);
                    } }), error ? (_jsxs(Text, { variant: "error", sx: {
                        bg: "var(--background-error)",
                        mt: 1,
                        p: 1,
                        borderRadius: "default"
                    }, children: ["Failed to download image: ", error.toLowerCase(), "."] })) : (_jsx(Text, { variant: "subBody", sx: {
                        bg: "shade",
                        color: "accent",
                        mt: 1,
                        p: 1,
                        borderRadius: "default"
                    }, children: "To protect your privacy, we will download the image & add it to your attachments." }))] }) }));
}
