import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_qrcode__ from "qrcode";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router__ from "react-router";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../../schema-settings/index.mjs";
const useShareActions = (param)=>{
    let { title, uid } = param;
    var _currentRoute_params;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const isAdmin = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/admin/:name');
    const isShare = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/share/:name');
    const isMobile = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/mobile/:name');
    const isMobilePage = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useIsMobile)();
    const currentRoute = isAdmin || isShare || isMobile;
    const replaceLink = isMobilePage ? window.location.href.replace('/mobile', '/share') : window.location.href.replace('/admin', '/share');
    const link = replaceLink.replace(null == currentRoute ? void 0 : null === (_currentRoute_params = currentRoute.params) || void 0 === _currentRoute_params ? void 0 : _currentRoute_params.name, uid || fieldSchema['x-uid']).replace(window.location.search || '', '');
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [qrLink, setQrLink] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    __WEBPACK_EXTERNAL_MODULE_qrcode__["default"].toDataURL(link, {
        width: 170
    }, (err, url)=>{
        if (err) {
            console.error(err);
            return;
        }
        if (!qrLink) setQrLink(url);
    });
    const copyLink = ()=>{
        navigator.clipboard.writeText(link).then((res)=>{
            __WEBPACK_EXTERNAL_MODULE_antd__.message.open({
                type: 'success',
                content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: [
                        t('Replicated'),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                            style: {
                                color: '#3279FE'
                            },
                            children: compile(title || '')
                        }),
                        t('page link')
                    ]
                })
            });
        });
    };
    const imageAction = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ImageModal, {
            link: qrLink,
            title: title
        });
    return {
        copyLink,
        imageAction
    };
};
const ImageModal = (props)=>{
    const { link, title } = props;
    const canvasRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { currentUserCtx } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.useCurrentUserVariable)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        canvasContent(canvasRef, link, currentUserCtx, t);
    }, []);
    const saveImage = ()=>{
        const canvas = canvasRef.current;
        if (canvas) {
            const imageData = canvas.toDataURL('image/png');
            const link = document.createElement('a');
            link.href = imageData;
            link.download = 'Tachybase-Share.png';
            link.click();
            __WEBPACK_EXTERNAL_MODULE_antd__.message.open({
                type: 'success',
                content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: [
                        t('Saved'),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                            style: {
                                color: '#3279FE'
                            },
                            children: compile(title || '')
                        }),
                        t('QR code')
                    ]
                })
            });
        }
    };
    const copyImage = ()=>{
        const canvas = canvasRef.current;
        if (canvas && navigator.clipboard) canvas.toBlob(async (blob)=>{
            if (!blob) return;
            try {
                const item = new ClipboardItem({
                    'image/png': blob
                });
                await navigator.clipboard.write([
                    item
                ]);
                __WEBPACK_EXTERNAL_MODULE_antd__.message.open({
                    type: 'success',
                    content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                        children: [
                            t('Replicated'),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                                style: {
                                    color: '#3279FE'
                                },
                                children: compile(title || '')
                            }),
                            t('QR code')
                        ]
                    })
                });
            } catch (err) {
                __WEBPACK_EXTERNAL_MODULE_antd__.message.error(t('Copy failed, please try again'));
            }
        }, 'image/png');
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        style: {
            marginTop: '23px',
            display: 'flex',
            flexDirection: 'column',
            justifyContent: 'center',
            alignItems: 'center'
        },
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("canvas", {
                ref: canvasRef,
                width: 240,
                height: 260,
                style: {
                    width: '240px',
                    height: '260px',
                    pointerEvents: 'none'
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                style: {
                    width: '100%',
                    display: 'flex',
                    justifyContent: 'space-between',
                    marginTop: '10px',
                    gap: '15px'
                },
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                        onClick: saveImage,
                        style: {
                            flex: 1
                        },
                        children: t('Save Picture')
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                        onClick: copyImage,
                        style: {
                            flex: 1
                        },
                        children: t('Copy QR code')
                    })
                ]
            })
        ]
    });
};
const canvasContent = (canvasRef, link, currentUserCtx, t)=>{
    const canvas = canvasRef.current;
    const ctx = canvas.getContext('2d');
    const dpr = window.devicePixelRatio;
    const { width, height } = canvas;
    canvas.width = Math.round(width * dpr);
    canvas.height = Math.round(height * dpr);
    canvas.style.width = width + 'px';
    canvas.style.height = height + 'px';
    ctx.scale(dpr, dpr);
    ctx.filter = 'blur(20px)';
    const backgroundGradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
    backgroundGradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
    backgroundGradient.addColorStop(1, 'rgba(234, 238, 246, 0.80)');
    ctx.fillStyle = backgroundGradient;
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    const drawGlow = (x, y)=>{
        const radius = 100;
        const gradient = ctx.createRadialGradient(x, y, 0, x, y, radius);
        gradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
        gradient.addColorStop(1, 'rgba(217, 238, 255, 0.216)');
        ctx.fillStyle = gradient;
        ctx.beginPath();
        ctx.arc(x, y, radius, 0, 2 * Math.PI);
        ctx.fill();
    };
    drawGlow(60, 30);
    drawGlow(190, 280);
    ctx.filter = 'none';
    ctx.fillStyle = '#AAAAAA';
    ctx.font = '13px Arial';
    ctx.textAlign = 'center';
    ctx.fillText("".concat(currentUserCtx.nickname), canvas.width / 4, 30);
    const img = new Image();
    img.src = link;
    img.onload = ()=>{
        const imgWidth = img.width;
        const imgHeight = img.height;
        const x = (canvas.width - imgWidth) / 8;
        const y = 40;
        const borderWidth = 3;
        const borderRadius = 20;
        ctx.beginPath();
        ctx.moveTo(x + borderRadius, y);
        ctx.arcTo(x + imgWidth, y, x + imgWidth, y + imgHeight, borderRadius);
        ctx.arcTo(x + imgWidth, y + imgHeight, x, y + imgHeight, borderRadius);
        ctx.arcTo(x, y + imgHeight, x, y, borderRadius);
        ctx.arcTo(x, y, x + imgWidth, y, borderRadius);
        ctx.closePath();
        ctx.clip();
        ctx.drawImage(img, x + borderWidth, y + borderWidth, imgWidth - 2 * borderWidth, imgHeight - 2 * borderWidth);
        ctx.lineWidth = borderWidth;
        ctx.strokeStyle = '#000';
        ctx.beginPath();
        ctx.moveTo(x + borderRadius, y);
        ctx.arcTo(x + imgWidth, y, x + imgWidth, y + imgHeight, borderRadius);
        ctx.arcTo(x + imgWidth, y + imgHeight, x, y + imgHeight, borderRadius);
        ctx.arcTo(x, y + imgHeight, x, y, borderRadius);
        ctx.arcTo(x, y, x + imgWidth, y, borderRadius);
        ctx.closePath();
        ctx.stroke();
    };
    ctx.fillStyle = '#AAAAAA';
    ctx.font = '13px Arial';
    ctx.textAlign = 'center';
    ctx.fillText(t('Scan the code to view the sharing'), canvas.width / 4, 230);
    ctx.fillStyle = 'rgba(50, 121, 254, 1)';
    ctx.font = '14px Arial';
    ctx.textAlign = 'center';
    ctx.fillText('灵矶(Tachybase)', canvas.width / 4, 250);
};
export { useShareActions };
