import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/*
* Copyright (c) 2020-present SMC Treviso s.r.l. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
import React from "react";
import { css } from "styled-components";
import { WebResult } from "../renderers/openk9/web/WebResult";
import { DocumentResult } from "../renderers/openk9/document/DocumentResult";
import { PdfResult } from "../renderers/openk9/pdf/PdfResult";
import { MobileLogoSvg } from "../svgElement/MobileLogoSvg";
import { useTranslation } from "react-i18next";
function Result(props) {
    const result = props.result;
    const { onDetail, renderers, isMobile, setIdPreview, setDetailMobile, viewButton, overChangeCard, setViewButtonDetail, templateCustom, } = props;
    const getCustomTemplate = () => {
        if (!templateCustom || templateCustom.length === 0)
            return null;
        const matchedTemplate = templateCustom.find((template) => result.source.documentTypes.includes(template.source));
        return matchedTemplate?.Template ?? null;
    };
    return (_jsx("div", { className: `openk9-embeddable-search--result-container openk9-card-${result?.source?.id}`, onMouseEnter: () => {
            if (overChangeCard && !isMobile) {
                onDetail(result);
                if (setIdPreview)
                    setIdPreview(result?.source?.id || "");
            }
        }, onClick: () => {
            if (!overChangeCard && !isMobile && !viewButton) {
                onDetail(result);
                if (setIdPreview)
                    setIdPreview(result?.source?.id || "");
            }
        }, css: css `
        cursor: ${!overChangeCard ? "pointer" : "auto"};
      `, children: (() => {
            const CustomTemplate = getCustomTemplate();
            if (CustomTemplate) {
                return (_jsxs(React.Fragment, { children: [_jsx(CustomTemplate, { ...result }), isMobile && (_jsx(CreateButton, { setIdPreview: setIdPreview, setDetailMobile: setDetailMobile, result: result }))] }));
            }
            const Renderer = result.source.documentTypes
                .map((k) => renderers?.resultRenderers[k])
                .find(Boolean);
            if (Renderer && typeof Renderer === "function") {
                return (_jsxs(React.Fragment, { children: [_jsx(Renderer, { result: result }), isMobile && (_jsx(CreateButton, { setIdPreview: setIdPreview, setDetailMobile: setDetailMobile, result: result })), viewButton && !isMobile && (_jsx(ButtonDetail, { result: result, onDetail: onDetail, setIdPreview: setIdPreview, setViewButtonDetail: setViewButtonDetail }))] }));
            }
            if (result.source.documentTypes.includes("pdf")) {
                return (_jsxs(React.Fragment, { children: [_jsx(PdfResult, { result: result }), isMobile && (_jsx(CreateButton, { setIdPreview: setIdPreview, setDetailMobile: setDetailMobile, result: result }))] }));
            }
            if (result.source.documentTypes.includes("document")) {
                return (_jsxs(React.Fragment, { children: [_jsx(DocumentResult, { result: result }), isMobile && (_jsx(CreateButton, { setIdPreview: setIdPreview, setDetailMobile: setDetailMobile, result: result })), viewButton && !isMobile && (_jsx(ButtonDetail, { result: result, onDetail: onDetail, setIdPreview: setIdPreview, setViewButtonDetail: setViewButtonDetail }))] }));
            }
            if (result.source.documentTypes.includes("web")) {
                return (_jsxs(React.Fragment, { children: [_jsx(WebResult, { result: result }), isMobile && (_jsx(CreateButton, { setIdPreview: setIdPreview, setDetailMobile: setDetailMobile, result: result })), viewButton && !isMobile && (_jsx(ButtonDetail, { result: result, onDetail: onDetail, setIdPreview: setIdPreview, setViewButtonDetail: setViewButtonDetail }))] }));
            }
            return (_jsxs("pre", { className: "openk9-embeddable-search--no-result-container", css: css `
              height: 100px;
              overflow: hidden;
            `, children: ["Not implemented", JSON.stringify(result, null, 2)] }));
        })() }));
}
function CreateButton({ setDetailMobile, result, setIdPreview, }) {
    const { t } = useTranslation();
    return (_jsx("div", { className: "openk9-wrapper-button-mobile", css: css `
        padding: 24px;
        padding-top: 8px;
        @media (max-width: 480px) {
          padding: 20px;
          padding-top: 4px;
        }
      `, children: _jsxs("button", { id: "preview-card-" + result?.source?.id, css: css `
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 3px;
          padding: 8px 16px;
          background-color: #f9edee;
          border: 1px solid #f9edee;
          border-radius: 50px;
          color: #c0272b;
          font-weight: 500;
          font-size: 13px;
          cursor: pointer;
        `, onClick: () => {
                if (setIdPreview)
                    setIdPreview(result?.source?.id || "");
                setDetailMobile(result);
            }, children: [_jsx("span", { children: t("preview") }), _jsx(MobileLogoSvg, {})] }) }));
}
function ButtonDetail({ result, onDetail, setIdPreview, setViewButtonDetail, }) {
    const { t } = useTranslation();
    return (_jsx("div", { className: "openk9-wrapper-button-mobile", css: css `
        padding: 24px;
        padding-top: 8px;
        @media (max-width: 480px) {
          padding: 20px;
          padding-top: 4px;
        }
      `, children: _jsxs("button", { id: `openk9-button-card-${result?.source?.id}`, className: "openk9-wrapper-button-mobile openk9-detail-web-button", css: css `
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 3px;
          padding: 8px 16px;
          background-color: #f9edee;
          border: 1px solid #f9edee;
          border-radius: 50px;
          color: #c0272b;
          font-weight: 500;
          font-size: 13px;
          cursor: pointer;
        `, onClick: (e) => {
                const recoveryButton = document.getElementById("title-preview-openk9");
                onDetail(result);
                if (e.screenX === 0 && e.screenY === 0) {
                    setViewButtonDetail && setViewButtonDetail(true);
                }
                else {
                    setViewButtonDetail && setViewButtonDetail(false);
                }
                if (setIdPreview)
                    setIdPreview(result?.source?.id || "");
                if (recoveryButton)
                    recoveryButton.focus();
            }, children: [_jsx("span", { children: t("preview") }), _jsx(MobileLogoSvg, {})] }) }));
}
export const ResultMemo = React.memo(Result);
