"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useShareActions: ()=>useShareActions
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_qrcode_namespaceObject = require("qrcode");
var external_qrcode_default = /*#__PURE__*/ __webpack_require__.n(external_qrcode_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const external_react_router_namespaceObject = require("react-router");
const index_js_namespaceObject = require("../../../../block-provider/index.js");
const external_index_js_namespaceObject = require("../../../index.js");
const external_schema_settings_index_js_namespaceObject = require("../../../../schema-settings/index.js");
const useShareActions = (param)=>{
    let { title, uid } = param;
    var _currentRoute_params;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const isAdmin = (0, external_react_router_namespaceObject.useMatch)('/admin/:name');
    const isShare = (0, external_react_router_namespaceObject.useMatch)('/share/:name');
    const isMobile = (0, external_react_router_namespaceObject.useMatch)('/mobile/:name');
    const isMobilePage = (0, index_js_namespaceObject.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, external_react_i18next_namespaceObject.useTranslation)();
    const [qrLink, setQrLink] = (0, external_react_namespaceObject.useState)();
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    external_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)=>{
            external_antd_namespaceObject.message.open({
                type: 'success',
                content: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        t('Replicated'),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                            style: {
                                color: '#3279FE'
                            },
                            children: compile(title || '')
                        }),
                        t('page link')
                    ]
                })
            });
        });
    };
    const imageAction = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ImageModal, {
            link: qrLink,
            title: title
        });
    return {
        copyLink,
        imageAction
    };
};
const ImageModal = (props)=>{
    const { link, title } = props;
    const canvasRef = (0, external_react_namespaceObject.useRef)(null);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { currentUserCtx } = (0, external_schema_settings_index_js_namespaceObject.useCurrentUserVariable)();
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    (0, external_react_namespaceObject.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();
            external_antd_namespaceObject.message.open({
                type: 'success',
                content: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        t('Saved'),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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
                ]);
                external_antd_namespaceObject.message.open({
                    type: 'success',
                    content: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                        children: [
                            t('Replicated'),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                style: {
                                    color: '#3279FE'
                                },
                                children: compile(title || '')
                            }),
                            t('QR code')
                        ]
                    })
                });
            } catch (err) {
                external_antd_namespaceObject.message.error(t('Copy failed, please try again'));
            }
        }, 'image/png');
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        style: {
            marginTop: '23px',
            display: 'flex',
            flexDirection: 'column',
            justifyContent: 'center',
            alignItems: 'center'
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("canvas", {
                ref: canvasRef,
                width: 240,
                height: 260,
                style: {
                    width: '240px',
                    height: '260px',
                    pointerEvents: 'none'
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                style: {
                    width: '100%',
                    display: 'flex',
                    justifyContent: 'space-between',
                    marginTop: '10px',
                    gap: '15px'
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                        onClick: saveImage,
                        style: {
                            flex: 1
                        },
                        children: t('Save Picture')
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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);
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
