import { jsx as e, jsxs as o } from "react/jsx-runtime";
import { useState as d } from "react";
import { y as I, ao as j, ap as M, j as T, z as g, Z as C, N as A, aq as E, ar as z, a4 as R } from "./index-_1f6rhg6.js";
const y = "search", k = "url", B = (s) => {
  const { t: a } = I(), [i, l] = d(""), [p, m] = d(0), [c, v] = d(""), { isOpenDialog: D, setIsOpenDialog: O } = s, [u, b] = d(!1), [N, h] = d(""), [x, _] = d(y), w = async (n, f) => {
    if (u)
      return;
    b(!0), h("");
    const t = await fetch("/api/integrations/giphy/search", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        keyword: n,
        offset: f
      })
    }), r = await t.json();
    return t.ok ? (l(r.image || ""), m(r.nextOffset), r.image || h("No Result found")) : h((r == null ? void 0 : r.message) || "Something went wrong"), b(!1), null;
  }, G = async (n) => {
    var r;
    if (u)
      return;
    b(!0), h("");
    const f = await fetch("/api/integrations/giphy/get", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        url: n
      })
    }), t = await f.json();
    return f.ok ? (l(t.image || ""), t.image || h("No Result found")) : h((t == null ? void 0 : t.message) || ((r = t == null ? void 0 : t[0]) == null ? void 0 : r.message) || "Something went wrong"), b(!1), null;
  }, S = (n, f, t) => /* @__PURE__ */ o(
    "div",
    {
      className: C(
        "flex cursor-pointer items-center border-b-2 p-2 text-sm ",
        x === t ? "text-default border-emphasis" : "text-subtle border-transparent"
      ),
      onClick: () => {
        v(""), l(""), _(t);
      },
      children: [
        /* @__PURE__ */ e(R, { name: n, className: "h-4 w-4 ltr:mr-2 rtl:ml-2" }),
        f
      ]
    }
  ), P = async (n) => {
    n.stopPropagation(), n.preventDefault(), x === y ? w(c, 0) : x === k && G(c);
  };
  return /* @__PURE__ */ e(j, { open: D, onOpenChange: O, children: /* @__PURE__ */ o(M, { children: [
    /* @__PURE__ */ e("h3", { className: "leading-16 font-cal text-emphasis text-xl", id: "modal-title", children: a("add_gif_to_confirmation") }),
    /* @__PURE__ */ e("p", { className: "text-subtle mb-3 text-sm font-light", children: a("find_gif_spice_confirmation") }),
    /* @__PURE__ */ o("div", { className: "border-emphasis flex items-center border-b border-solid", children: [
      S("search", a("search_giphy"), y),
      S("link", a("add_link_from_giphy"), k)
    ] }),
    /* @__PURE__ */ o(
      "form",
      {
        className: "flex w-full justify-center space-x-2 space-y-2 rtl:space-x-reverse",
        onSubmit: P,
        children: [
          /* @__PURE__ */ e("div", { className: "relative block w-full pt-2", children: /* @__PURE__ */ e(
            T,
            {
              type: "text",
              placeholder: x === y ? a("search_giphy") : "https://media.giphy.com/media/some-id/giphy.gif",
              value: c,
              onChange: (n) => {
                v(n.target.value);
              }
            }
          ) }),
          /* @__PURE__ */ e(g, { type: "submit", tabIndex: -1, color: "secondary", loading: u, children: a("search") })
        ]
      }
    ),
    i && /* @__PURE__ */ e("div", { className: "flex flex-col items-center space-x-2 space-y-2 pt-3 rtl:space-x-reverse", children: /* @__PURE__ */ e("div", { className: "bg-subtle flex w-full items-center justify-center", children: u ? /* @__PURE__ */ e("div", { className: "flex h-[200px] w-full items-center justify-center bg-gray-400 pb-3 pt-3", children: /* @__PURE__ */ o(
      "svg",
      {
        className: C("mx-4 h-5 w-5 animate-spin", "text-inverted dark:text-emphasis"),
        xmlns: "http://www.w3.org/2000/svg",
        fill: "none",
        viewBox: "0 0 24 24",
        children: [
          /* @__PURE__ */ e(
            "circle",
            {
              className: "opacity-25",
              cx: "12",
              cy: "12",
              r: "10",
              stroke: "currentColor",
              strokeWidth: "4"
            }
          ),
          /* @__PURE__ */ e(
            "path",
            {
              className: "opacity-75",
              fill: "currentColor",
              d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
            }
          )
        ]
      }
    ) }) : /* @__PURE__ */ e("img", { className: "h-[200px] pb-3 pt-3", src: i, alt: `Gif from Giphy for ${c}` }) }) }),
    N && /* @__PURE__ */ e(A, { severity: "error", title: N, className: "my-4" }),
    i && x === y && /* @__PURE__ */ o("div", { className: "mt-4 flex items-center justify-between space-x-2 rtl:space-x-reverse", children: [
      /* @__PURE__ */ e("div", { className: "text-subtle text-sm font-light", children: "Not the perfect GIF?" }),
      /* @__PURE__ */ e(
        g,
        {
          size: "sm",
          color: "secondary",
          type: "button",
          loading: u,
          onClick: () => w(c, p),
          children: "Shuffle"
        }
      )
    ] }),
    /* @__PURE__ */ o(E, { children: [
      /* @__PURE__ */ e(
        z,
        {
          color: "minimal",
          tabIndex: -1,
          onClick: () => {
            s.setIsOpenDialog(!1);
          },
          children: a("cancel")
        }
      ),
      /* @__PURE__ */ e(
        g,
        {
          type: "button",
          disabled: !i,
          onClick: () => (s.setIsOpenDialog(!1), s.onSave(i), m(0), l(""), v(""), !1),
          children: a("add_gif")
        }
      )
    ] })
  ] }) });
};
function F(s) {
  const { t: a } = I(), [i, l] = d(s.defaultValue), [p, m] = d(!1);
  return /* @__PURE__ */ o("div", { className: "flex flex-col items-start space-x-2 space-y-2 rtl:space-x-reverse", children: [
    i && /* @__PURE__ */ e("div", { className: "min-h-[200px]", children: /* @__PURE__ */ e("img", { alt: "Selected Gif Image", src: i }) }),
    /* @__PURE__ */ o("div", { className: "flex", children: [
      i ? /* @__PURE__ */ e(
        g,
        {
          color: "minimal",
          type: "button",
          StartIcon: "pencil",
          onClick: () => m(!0),
          disabled: s.disabled,
          children: "Change"
        }
      ) : /* @__PURE__ */ e(
        g,
        {
          color: "minimal",
          type: "button",
          StartIcon: "plus",
          onClick: () => m(!0),
          disabled: s.disabled,
          children: "Add from Giphy"
        }
      ),
      i && /* @__PURE__ */ e(
        g,
        {
          color: "destructive",
          type: "button",
          StartIcon: "x",
          onClick: () => {
            l(""), s.onChange("");
          },
          disabled: s.disabled,
          children: a("remove")
        }
      )
    ] }),
    /* @__PURE__ */ e(
      B,
      {
        isOpenDialog: p,
        setIsOpenDialog: m,
        onSave: (c) => {
          l(c), s.onChange(c);
        }
      }
    )
  ] });
}
const J = ({
  getAppData: s,
  setAppData: a,
  disabled: i
}) => {
  const l = s("thankYouPage");
  return /* @__PURE__ */ e(
    F,
    {
      defaultValue: l,
      disabled: i,
      onChange: (p) => {
        a("thankYouPage", p);
      }
    }
  );
};
export {
  J as default
};
