/* global React */
const { useState, useEffect, useRef } = React;

// ===================== TWEAKS HOOKS =====================
function useTweaks(defaults) {
  const [tweaks, setTweaksState] = useState(defaults);
  const setTweaks = (keyOrObj, val) => {
    const edits = typeof keyOrObj === "string" ? { [keyOrObj]: val } : keyOrObj;
    setTweaksState((t) => ({ ...t, ...edits }));
    try {
      window.parent.postMessage({ type: "__edit_mode_set_keys", edits }, "*");
    } catch (e) {}
  };
  return [tweaks, setTweaks];
}

function useEditModeProtocol(setEditMode) {
  useEffect(() => {
    const onMsg = (e) => {
      const t = e.data?.type;
      if (t === "__activate_edit_mode") setEditMode(true);
      if (t === "__deactivate_edit_mode") setEditMode(false);
    };
    window.addEventListener("message", onMsg);
    try { window.parent.postMessage({ type: "__edit_mode_available" }, "*"); } catch (e) {}
    return () => window.removeEventListener("message", onMsg);
  }, []);
}

// Simulated reply text generator (varies based on input)
function simulatedReply(text) {
  const t = text.toLowerCase();
  if (t.includes("python") || t.includes("code") || t.includes("script")) {
    return "Here's a small Python sketch that does what you described. I've kept it dependency-free so you can run it anywhere with Python 3.\n\nThe core idea: read a list of prompts, pick one at random, then loop until the user types 'quit'. I added a `--shuffle` flag in case you want to randomize order without repeats.\n\nWhen you run this, you'll see one prompt at a time. Press Enter to skip; type your answer to compare against the key. Let me know if you'd like me to add scoring or persistence to a file.";
  }
  if (t.includes("explain") || t.includes("why")) {
    return "Great question. The short answer is that this is one of those phenomena where the everyday intuition is almost right but missing one mechanism.\n\nLet me build it up in three layers:\n\nFirst, the surface-level reason — the part most people remember. Then the underlying mechanism, which is where the actual physics lives. Finally a useful counter-example, because if you can predict where the rule breaks down, you really understand it.\n\nWant me to keep going on any of these layers, or try a different angle — maybe an analogy, or a worked numeric example?";
  }
  if (t.includes("history") || t.includes("war")) {
    return "Historians usually frame this as a convergence of long-running structural pressures and a few contingent triggers — meaning even small changes to a single decision could have produced a very different outcome.\n\nThree threads to keep separate as we go: economic, political, and ideological. They reinforced each other, but they moved at different speeds. The economic shifts had been building for decades; the political crisis unfolded over a few years; the spark itself happened in a matter of weeks.\n\nWhich angle would you like to start with?";
  }
  return "Happy to help with that. Let me think out loud for a moment so you can see the shape of the answer before I commit to one direction.\n\nThere are a few useful framings here. The first is the textbook approach, which gets you 80% of the way and is what most courses test. The second is the intuition-first approach, which is slower at first but tends to stick longer. The third is the historical approach — *why* this idea was invented in the first place, which often makes it click.\n\nWhich would land best for you right now?";
}

window.useTweaks = useTweaks;
window.useEditModeProtocol = useEditModeProtocol;
window.simulatedReply = simulatedReply;
