import { useEditorRef as y, useHotkeys as h, getPluginOptions as m } from "@udecode/plate-core";
import { floatingLinkSelectors as i, floatingLinkActions as a, submitFloatingLink as b, ELEMENT_LINK as E } from "@udecode/plate-link";
import { useEffect as U, useReducer as k } from "react";
import { getLegacyUrl as x, getUrl as w } from "../../utils/getUrl.es.js";
import { CheckboxState as o } from "../../../../../Checkbox/Checkbox.es.js";
const I = {
  url: "",
  text: "",
  newTab: o.Unchecked
}, L = () => {
  const [r, n] = k((t, s) => {
    const { type: c, payload: l } = s;
    switch (c) {
      case "NEW_TAB":
        return {
          ...t,
          newTab: o.Checked
        };
      case "SAME_TAB":
        return {
          ...t,
          newTab: o.Unchecked
        };
      case "URL":
      case "TEXT":
      case "INIT":
        return {
          ...t,
          ...l
        };
      default:
        return t;
    }
  }, I);
  return [r, n];
}, _ = () => {
  const r = y(), [n, t] = L();
  U(() => {
    const e = x(r), f = w(r), g = i.newTab();
    t({
      type: "INIT",
      payload: {
        text: i.text(),
        newTab: g ? o.Checked : o.Unchecked,
        url: e && f === "" ? e : i.url()
      }
    });
  }, [t, r]);
  const s = (e) => {
    t({
      type: "TEXT",
      payload: { text: e }
    });
  }, c = (e) => {
    t({
      type: "URL",
      payload: { url: e }
    });
  }, l = (e) => {
    t(e ? { type: "NEW_TAB" } : { type: "SAME_TAB" });
  }, d = () => {
    a.reset();
  }, u = (e) => {
    !p() || !T || (a.text(n.text), a.url(n.url), a.newTab(n.newTab === o.Checked), b(r) && (e == null || e.preventDefault()));
  }, T = n.url !== "" && n.text !== "", p = () => {
    const { isUrl: e } = m(r, E);
    return !n.url || e && e(n.url);
  };
  return h(
    "enter",
    u,
    {
      enableOnFormTags: ["INPUT"]
    },
    []
  ), { state: n, onTextChange: s, onUrlChange: c, onToggleTab: l, onCancel: d, onSave: u, hasValues: T, isValidUrlOrEmpty: p };
};
export {
  L as InsertModalState,
  _ as useInsertModal
};
//# sourceMappingURL=useInsertModal.es.js.map
