import { Y as P, e as j, j as o, N as k, o as y, ah as A, p as h, a as H, b as R, a2 as E, a3 as _, s as B } from "./index-c65ca4a4.mjs";
import { C as O } from "./CheckboxGroup-61a40f23.mjs";
import { C as F } from "./ColorIndicator-6c9a0d02.mjs";
import { F as N } from "./Form-f4ced0f0.mjs";
import { H as U } from "./HtmlField-d23c9880.mjs";
import { P as I } from "./PreviewModal-1c377ea9.mjs";
import { d as z } from "./debounce-972af544.mjs";
import "./MobileChrome-2e9f6901.mjs";
const G = (a, s) => {
  const e = new URLSearchParams();
  return e.append("announcement_bg", a || "accent"), e.append("announcement", s || ""), e.append("announcement_vis", "paid_members"), e.toString();
}, $ = ({ announcementBackgroundColor: a, announcementContent: s, url: e }) => {
  const i = P(null);
  return j(() => {
    e && fetch(e, {
      method: "POST",
      headers: {
        "Content-Type": "text/html;charset=utf-8",
        "x-ghost-preview": G(
          a,
          s
        ),
        Accept: "text/plain",
        mode: "cors",
        credentials: "include"
      }
    }).then((m) => m.text()).then((m) => {
      var f, b, v;
      const l = "html { pointer-events: none; }", r = new DOMParser().parseFromString(m, "text/html"), c = r.querySelector("style"), p = c.innerHTML;
      c.innerHTML = `${p}

${l}`;
      let g = (r.doctype ? new XMLSerializer().serializeToString(r.doctype) : "") + r.documentElement.outerHTML;
      const d = i.current;
      d && ((f = d.contentDocument) == null || f.open(), (b = d.contentDocument) == null || b.write(g), (v = d.contentDocument) == null || v.close());
    }).catch(() => {
    });
  }, [a, s, e]), /* @__PURE__ */ o.jsx(o.Fragment, { children: /* @__PURE__ */ o.jsx(
    "iframe",
    {
      ref: i,
      "data-testid": "announcement-bar-preview",
      height: "100%",
      title: "Announcement Bar Preview",
      width: "100%"
    }
  ) });
}, J = ({
  announcementContent: a,
  announcementTextHandler: s,
  accentColor: e,
  announcementBackgroundColor: i,
  toggleColorSwatch: m,
  toggleVisibility: l,
  visibility: u = [],
  paidMembersEnabled: r
}) => {
  const { config: c } = y(), p = [
    {
      label: "Logged out visitors",
      onChange: (t) => {
        l("visitors", t);
      },
      value: "visitors",
      checked: u.includes("visitors")
    },
    {
      label: "Free members",
      onChange: (t) => {
        l("free_members", t);
      },
      value: "free_members",
      checked: u.includes("free_members")
    },
    ...r ? [{
      label: "Paid members",
      onChange: (t) => {
        l("paid_members", t);
      },
      value: "paid_members",
      checked: u.includes("paid_members")
    }] : []
  ];
  return /* @__PURE__ */ o.jsxs(N, { children: [
    /* @__PURE__ */ o.jsx(
      U,
      {
        config: c,
        nodes: "MINIMAL_NODES",
        placeholder: "Highlight breaking news, offers or updates",
        title: "Announcement",
        value: a,
        onChange: s
      }
    ),
    /* @__PURE__ */ o.jsx(
      F,
      {
        isExpanded: !1,
        picker: !1,
        swatches: [
          {
            hex: "#08090c",
            title: "Dark",
            value: "dark"
          },
          {
            hex: "#ffffff",
            title: "Light",
            value: "light"
          },
          {
            hex: e || "#ffdd00",
            title: "Accent",
            value: "accent"
          }
        ],
        swatchSize: "lg",
        title: "Background color",
        value: i,
        onSwatchChange: (t) => {
          t !== null && m(t);
        },
        onTogglePicker: () => {
        }
      }
    ),
    /* @__PURE__ */ o.jsx(
      O,
      {
        checkboxes: p,
        title: "Visibility"
      }
    )
  ] });
}, q = () => {
  const { siteData: a } = y(), s = k.useModal(), { localSettings: e, updateSetting: i, handleSave: m } = A(), [l] = h(e, ["announcement_content"]), [u] = h(e, ["accent_color"]), [r] = h(e, ["announcement_background"]), [c] = h(e, ["announcement_visibility"]), [p] = h(e, ["paid_members_enabled"]), t = JSON.parse((c == null ? void 0 : c.toString()) || "[]"), { updateRoute: g } = H(), [d, f] = R("homepage"), b = (n) => {
    i("announcement_background", n);
  }, v = (n, M) => {
    const C = t.indexOf(n);
    C === -1 && M ? t.push(n) : t.splice(C, 1), i("announcement_visibility", JSON.stringify(t));
  }, T = P(
    z((n) => {
      i("announcement_content", n);
    }, 500)
  ), L = /* @__PURE__ */ o.jsx(
    J,
    {
      accentColor: u,
      announcementBackgroundColor: r,
      announcementContent: l,
      announcementTextHandler: (n) => {
        T.current(n);
      },
      paidMembersEnabled: p,
      toggleColorSwatch: b,
      toggleVisibility: v,
      visibility: c
    }
  ), { data: { posts: [w] } = { posts: [] } } = E({
    searchParams: {
      filter: "status:published",
      order: "published_at DESC",
      limit: "1",
      fields: "id,url"
    }
  });
  let S = [];
  w && (S = [
    { id: "homepage", title: "Homepage" },
    { id: "post", title: "Post" }
  ]);
  const D = (n) => {
    S.length && f(n);
  };
  let x = _(a);
  switch (d) {
    case "homepage":
      x = _(a);
      break;
    case "post":
      x = w.url;
      break;
  }
  return /* @__PURE__ */ o.jsx(
    I,
    {
      afterClose: () => {
        s.remove(), g("announcement-bar");
      },
      cancelLabel: "Close",
      deviceSelector: !1,
      dirty: !1,
      okLabel: "Save",
      preview: /* @__PURE__ */ o.jsx($, { announcementBackgroundColor: r, announcementContent: l, url: x }),
      previewBgColor: "greygradient",
      previewToolbarTabs: S,
      selectedURL: d,
      sidebar: L,
      testId: "announcement-bar-modal",
      title: "Announcement",
      titleHeadingLevel: 5,
      onOk: async () => {
        await m() ? (s.remove(), g("announcement-bar")) : B({
          type: "pageError",
          message: "An error occurred while saving your changes. Please try again."
        });
      },
      onSelectURL: D
    }
  );
}, te = k.create(q);
export {
  te as default
};
//# sourceMappingURL=AnnouncementBarModal-477ad0f3.mjs.map
