import { jsx as t, jsxs as l } from "react/jsx-runtime";
import V, { useRef as K, useState as O, useEffect as X } from "react";
import { Container as x, Grid as C, Button as Y, Dropdown as A, Icon as Z } from "semantic-ui-react";
import { PostContent as L } from "@devgateway/wp-react-lib";
import { domtoimage as G } from "./dom-to-image.js";
import { saveAs as U } from "file-saver";
import { connect as _ } from "react-redux";
const ee = V.forwardRef((a, r) => /* @__PURE__ */ t("div", { ref: r, children: a.children })), te = (a) => {
  const r = K();
  let {
    childContent: h,
    "data-height": re,
    "data-button-label": E,
    "data-png-label": J,
    "data-jpg-label": T,
    "data-jpg-text": F,
    "data-png-text": S,
    "data-check-png": y = "true",
    "data-check-jpg": k = "true",
    "data-title": I,
    "data-default-format": w = "PNG",
    "data-use-title": P = "false",
    "data-style": B = "heavy",
    "data-section-title": $ = "",
    "data-download-tooltip": j = "",
    "data-include-source-url": q = "false",
    "data-include-filters": R = "false",
    "data-source-urlmargin-left": D = 70,
    "data-source-urlmargin-top": W = 10,
    "data-source-urlfont-size": M = 18,
    parent: ae,
    editing: i,
    component: ie,
    unique: le,
    pageModuleProps: c
  } = a;
  const [p, m] = O(w), s = y == "true" || y === !0, d = k == "true" || k === !0;
  X(() => {
    m(w);
  }, [w]);
  const v = (n) => {
    m(n.target.value);
  };
  function z(n) {
    const o = n.attributes, N = [];
    if (o)
      for (let e = 0; e < o.length; e++)
        N.push(o[e].nodeName);
    const u = N.filter((e) => e.startsWith("data-"));
    if (u.length > 0 && u.forEach((e) => {
      n.setAttribute(e, "");
    }), n.classList) {
      if (n.classList.contains("ignore")) return !1;
      if (!(R === !0 || R === "true")) {
        const f = [
          "filter-component",
          "data-filters-reset",
          "data-filters-apply",
          "filter-search"
        ];
        for (const Q of f)
          if (n.classList.contains(Q)) return !1;
      }
      return !0;
    }
    return !0;
  }
  const g = { filter: z, bgcolor: "#FFF", imagePlaceholder: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAoMBgQb9Z3kAAAAASUVORK5CYII=", cacheBust: !0 }, H = (n) => {
    G.cloneNode(r.current).then(function(o) {
      if ([...o.getElementsByTagName("input")].forEach((e) => e.remove()), [...o.querySelectorAll(".question.circle.icon")].forEach((e) => e.remove()), q === "true") {
        const e = document.createElement("div");
        e.style.marginLeft = D + "px", e.style.marginTop = W + "px", e.style.fontSize = M + "px", e.style.fontFamily = "Roboto, sans-serif", e.style.fontWeight = "400", e.style.color = "#66676d", e.style.opacity = "0.75", e.innerHTML = window.location.href, e.style.maxWidth = "90%", e.style.wordWrap = "break-word", e.style.overflowWrap = "break-word", o.appendChild(e);
      }
      const u = 50;
      g.height = r.current.scrollHeight + u, g.width = r.current.scrollWidth + u * 2, o.style.padding = `${u}px`, n === "PNG" && G.toPng(o, g).then(function(e) {
        if (!e) throw new Error("PNG render returned empty result");
        return fetch(e).then((f) => f.blob());
      }).then(function(e) {
        U(e, J);
      }).catch(function(e) {
        console.error("PNG download failed:", e);
      }), n === "JPG" && G.toJpeg(o, g).then(function(e) {
        if (!e) throw new Error("JPEG render returned empty result");
        return fetch(e).then((f) => f.blob());
      }).then(function(e) {
        U(e, T);
      }).catch(function(e) {
        console.error("JPG download failed:", e);
      });
    });
  }, b = (n) => {
    i ? alert("Not allowed when editing please preview page") : H(n);
  };
  return /* @__PURE__ */ t(
    x,
    {
      className: `viz download ${B} ${P ? "has-title" : ""}
        ${s || d ? "has-formats" : ""}
        ${i || c != null && c.editing ? "editing" : ""}
        ${s && d ? "multi-format" : ""}
        ${s && !d || !s && d ? "single-format" : ""}`,
      fluid: !0,
      children: /* @__PURE__ */ l(ee, { ref: r, children: [
        /* @__PURE__ */ l(C, { stackable: !0, reversed: "mobile", className: "download-header", children: [
          !i && P === "true" && /* @__PURE__ */ t(C.Column, { width: 12, children: /* @__PURE__ */ t(
            L,
            {
              parentUnique: a.unique,
              post: { content: { rendered: decodeURIComponent($) } }
            }
          ) }),
          /* @__PURE__ */ t(
            C.Column,
            {
              className: i ? "editing ignore" : "ignore",
              width: i || P !== "true" ? 16 : 4,
              textAlign: "right",
              children: /* @__PURE__ */ l("div", { className: "wrapper", children: [
                /* @__PURE__ */ l(Y, { className: "download", onClick: () => b(p), children: [
                  E,
                  " ",
                  p === "PNG" ? "PNG" : "JPG"
                ] }),
                /* @__PURE__ */ t(
                  A,
                  {
                    className: "download",
                    "data-tooltip": decodeURIComponent(j),
                    trigger: d && s ? /* @__PURE__ */ t(Z, { name: "download", className: "download-icon" }) : null,
                    children: /* @__PURE__ */ l(A.Menu, { children: [
                      I,
                      s === !0 && /* @__PURE__ */ l(A.Item, { onClick: () => {
                        m("PNG"), b("PNG");
                      }, children: [
                        /* @__PURE__ */ t("input", { type: "radio", value: "PNG", checked: p === "PNG", onChange: v }),
                        /* @__PURE__ */ t("label", { children: S })
                      ] }),
                      d === !0 && /* @__PURE__ */ l(A.Item, { onClick: () => {
                        m("JPG"), b("JPG");
                      }, children: [
                        /* @__PURE__ */ t("input", { type: "radio", value: "JPG", checked: p === "JPG", onChange: v }),
                        /* @__PURE__ */ t("label", { children: F })
                      ] })
                    ] })
                  }
                )
              ] })
            }
          )
        ] }),
        !i && /* @__PURE__ */ t(x, { fluid: !0, className: `download area ${i || c != null && c.editing ? "editing" : ""}`, children: /* @__PURE__ */ t(
          L,
          {
            parentUnique: a.unique,
            post: { content: { rendered: h } }
          }
        ) })
      ] })
    }
  );
}, ne = (a) => {
  const r = a.getIn(["data", "pageModuleProps"]), h = {};
  return r && (h.pageModuleProps = r), h;
}, oe = {}, ge = _(ne, oe)(te);
export {
  ge as default
};
