import { useState as p, useRef as v, useMemo as k, useCallback as c, useEffect as $ } from "react";
import { DEFAULT_COLOR_PALETTE as w } from "../constants/index.js";
function O(r, x, d, C) {
  const f = C ?? w, [n, m] = p("text"), [l, u] = p("000000"), s = v(null), t = v(null), a = n === "text" ? x : d, g = k(() => l ? `#${l}` : a ?? "#000000", [l, a]), T = c((e) => {
    m(e), u("");
  }, []), E = c(
    (e) => {
      r(n === "text" ? "textColor" : "bgColor", { color: e }), u(e.replace("#", ""));
    },
    [n, r]
  ), b = c(() => {
    if (l.length !== 6) return;
    r(n === "text" ? "textColor" : "bgColor", { color: `#${l}` });
  }, [n, l, r]), L = c(() => {
    r(n === "text" ? "resetTextColor" : "resetBgColor"), u("");
  }, [n, r]), H = c((e) => {
    const o = e.target.value.replace(/[^0-9a-fA-F]/g, "").slice(0, 6);
    u(o);
  }, []), I = c((e) => {
    const o = e.target.value.replace("#", "");
    u(o);
  }, []);
  return $(() => {
    const e = s.current;
    if (!e) return;
    const o = () => {
      const R = e.value.replace("#", "");
      r(n === "text" ? "textColor" : "bgColor", { color: `#${R}` }), t.current !== null && (clearTimeout(t.current), t.current = null);
    }, i = () => {
      t.current !== null && (clearTimeout(t.current), t.current = null);
    }, h = () => {
      t.current = setTimeout(() => {
        t.current = null;
      }, 150);
    };
    return e.addEventListener("change", o), e.addEventListener("input", i), e.addEventListener("blur", h), () => {
      e.removeEventListener("change", o), e.removeEventListener("input", i), e.removeEventListener("blur", h), t.current !== null && (clearTimeout(t.current), t.current = null);
    };
  }, [n, r]), {
    activeTab: n,
    palette: f,
    activeColor: a,
    previewHex: g,
    hexInput: l,
    colorInputRef: s,
    handleTabChange: T,
    handleSwatchClick: E,
    handleHexApply: b,
    handleReset: L,
    handleHexChange: H,
    handleColorPickerInput: I
  };
}
export {
  O as default
};
//# sourceMappingURL=useHtmlEditorTextColor.js.map
