/* appmenu.jsx: the one app menu every Clubcraft app uses (second level, beside the icon rail).
 *   <CCAppMenu title icon top groups active foot />
 *   groups: [{ label?, items: [{ key, label, icon?, count?, badge?, onClick, disabled?, hidden?, title? }] }]
 * `icon` / item icons are SVG path data (24x24, stroked), so apps do not need their own icon components. */
(function () {
  const Svg = ({ d }) => <svg viewBox="0 0 24 24" aria-hidden="true">{[].concat(d).map((p, i) => <path key={i} d={p} />)}</svg>;
  function AppMenu({ title, icon, iconNode, top, groups, active, foot, label }) {
    return (
      <nav className="cc-appmenu" aria-label={(label || title) + " menu"}>
        <div className="cc-appmenu-h">{iconNode || (icon && <Svg d={icon} />)}<span>{title}</span></div>
        {top && <div className="cc-appmenu-top">{top}</div>}
        {groups.filter((g) => g.items.some((i) => !i.hidden)).map((g, gi) => (
          <div key={gi} className="cc-appmenu-g">
            {g.label && <div className="cc-appmenu-gl">{g.label}</div>}
            {g.items.filter((i) => !i.hidden).map((i) => (
              <button key={i.key} type="button" className={"cc-appmenu-i" + (active === i.key ? " on" : "")} disabled={i.disabled} title={i.title}
                aria-current={active === i.key ? "page" : undefined} onClick={i.onClick}>
                <span className="ico">{i.icon && <Svg d={i.icon} />}</span><span className="lbl">{i.label}</span>
                {i.count != null && <small className="cnt">{i.count}</small>}{i.badge ? <small className="bdg">{i.badge}</small> : null}
              </button>
            ))}
          </div>
        ))}
        {foot && <div className="cc-appmenu-foot">{foot}</div>}
      </nav>
    );
  }
  window.CCAppMenu = AppMenu;

  // "Room to grow": the note at the top of an app saying which Zoho product it can integrate with or move
  // to. One component for every app so they all look and behave the same.
  //  - First time: the full box, with Hide. Hide snoozes it for 60 days.
  //  - After that: a slim one-line chip that can be opened to the full text. Hide snoozes it again.
  //  - 180 days after the first hide the chip also offers "Don't show again".
  // The state is kept per member on the server (PUT /club/me/notes). `force` shows the full box with no
  // buttons (the How to get started view), so the note can always be found again.
  const ROOM = {
    Tasks: ["Room to grow", "Need advanced project control? Tasks can integrate with, or later move to, Zoho Sprints.", "advanced project control: Zoho Sprints"],
    Social: ["Room to grow", "Need detailed marketing? Social can integrate with, or later move to, Zoho Marketing Automation or Zoho Social.", "detailed marketing: Zoho Marketing Automation or Zoho Social"],
    Calendar: ["Room to grow", "Need advanced calendar control? Calendar can integrate with, or later move to, Zoho Calendar.", "advanced calendar control: Zoho Calendar"],
    Contacts: ["Room to grow", "Need advanced contact management? Contacts can integrate with, or later move to, Zoho CRM.", "advanced contact management: Zoho CRM"],
    Finances: ["Works with your other apps", "Clubcraft will integrate with the other apps where it helps, for example an event's profit flowing into the books.", "integrates with your other apps"]
  };
  const SNOOZE_DAYS = 60, NEVER_AFTER_DAYS = 180;
  const dayNow = () => Math.floor(Date.now() / 86400000);
  // Shared by every note on the page: { loaded, notes: {Tasks: [firstHiddenDay, snoozeUntilDay, never]}, club }
  const roomState = { loaded: false, loading: false, notes: {}, club: null };
  const roomSubs = new Set();
  const roomEmit = () => roomSubs.forEach((f) => f());
  const roomDemo = () => !!(window.CCDemo && window.CCDemo.on);
  function roomLoad() {
    const club = window.CC_ACTIVE_CLUB || null;
    if (roomState.loaded && roomState.club === club) return;
    if (roomState.loading) return;
    roomState.club = club;
    if (roomDemo() || !club || !window.ClubcraftApi) { roomState.notes = {}; roomState.loaded = true; roomEmit(); return; }
    roomState.loading = true;
    window.ClubcraftApi.call("GET", "/club/me/notes", null, { club })
      .then((r) => { roomState.notes = (r && r.notes) || {}; })
      .catch(() => { roomState.notes = {}; })
      .then(() => { roomState.loading = false; roomState.loaded = true; roomEmit(); });
  }
  function roomSet(app, action) {
    const today = dayNow(), cur = roomState.notes[app] || [0, 0, 0];
    roomState.notes = { ...roomState.notes, [app]: action === "never" ? [cur[0] || today, cur[1], 1] : [cur[0] || today, today + SNOOZE_DAYS, cur[2]] };
    roomEmit();
    if (roomDemo() || !roomState.club || !window.ClubcraftApi) return;
    window.ClubcraftApi.call("PUT", "/club/me/notes", { app, action }, { club: roomState.club })
      .then((r) => { if (r && r.notes) { roomState.notes = r.notes; roomEmit(); } })
      .catch(() => { /* the note stays hidden for this visit; the next visit asks the server again */ });
  }
  function RoomNote({ app, force, style }) {
    const [, tick] = React.useState(0);
    const [open, setOpen] = React.useState(false);
    React.useEffect(() => {
      const f = () => tick((n) => n + 1);
      roomSubs.add(f); roomLoad();
      return () => roomSubs.delete(f);
    }, [app]);
    const note = ROOM[app];
    if (!note) return null;
    if (force) return <div className="cc-room" style={style}><b>{note[0]}</b><span>{note[1]}</span></div>;
    if (!roomState.loaded) return null;
    const rec = roomState.notes[app], today = dayNow();
    if (rec && (rec[2] || today < rec[1])) return null;
    const canNever = !!(rec && rec[0] && today - rec[0] >= NEVER_AFTER_DAYS);
    const hide = <button type="button" className="cc-room-x" onClick={() => roomSet(app, "snooze")} aria-label={"Hide the " + note[0] + " note for a while"} title={"Hide for " + SNOOZE_DAYS + " days"}>Hide ✕</button>;
    const never = canNever && <button type="button" className="cc-room-x never" onClick={() => roomSet(app, "never")}>Don't show again</button>;
    // Back after a snooze: a slim chip, opened on request.
    if (rec && !open) {
      return (
        <div className="cc-room chip" style={style}>
          <button type="button" className="cc-room-open" onClick={() => setOpen(true)} aria-expanded="false"><b>{note[0]}</b><span>{note[2]}</span><i aria-hidden="true">▾</i></button>
          <div className="cc-room-btns">{never}{hide}</div>
        </div>
      );
    }
    return (
      <div className="cc-room" style={style}>
        <b>{note[0]}</b><span>{note[1]}</span>
        {never}{hide}
      </div>
    );
  }
  window.CCRoomNote = RoomNote;
  // Shared icon paths so every app draws the same symbol for the same job.
  window.CCMenuIcons = {
    plan: ["M4 5h7v7H4zM13 5h7v4h-7zM13 11h7v8h-7zM4 14h7v5H4z"], doc: ["M7 3h8l4 4v14H7z", "M15 3v4h4M10 12h6M10 16h6"], check: ["M4 12l5 5 11-11"],
    history: ["M3 12a9 9 0 1 0 3-6.7L3 8", "M3 3v5h5M12 8v5l3 2"], calendar: ["M4 6h16v14H4zM4 10h16M8 3v4M16 3v4"], setup: ["M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z", "M19 12a7 7 0 0 0-.1-1.2l2-1.5-2-3.4-2.3.9a7 7 0 0 0-2-1.2L14.3 3h-4l-.4 2.6a7 7 0 0 0-2 1.2l-2.3-.9-2 3.4 2 1.5A7 7 0 0 0 5 12c0 .4 0 .8.1 1.2l-2 1.5 2 3.4 2.3-.9c.6.5 1.3.9 2 1.2l.4 2.6h4l.4-2.6c.7-.3 1.4-.7 2-1.2l2.3.9 2-3.4-2-1.5c.1-.4.1-.8.1-1.2z"],
    guide: ["M5 4h10a3 3 0 0 1 3 3v13H8a3 3 0 0 1-3-3z", "M8 8h6M8 12h6"], team: ["M9 11a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM3 20c0-3 3-5 6-5s6 2 6 5", "M16 5a3 3 0 0 1 0 6M18 15c2 .6 3 2.2 3 5"],
    palette: ["M12 21a9 9 0 1 1 9-9c0 3-2 3-4 3h-2a2 2 0 0 0-1 3.7c.5.5.3 2.3-2 2.3z", "M7.5 11h.01M10 7h.01M14.5 7h.01"], images: ["M3 6h18v14H3z", "M8 12a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3zM21 17l-5-5-4 4-2-2-7 7"],
    shield: ["M12 3l7 3v6c0 4.5-3 7.5-7 9-4-1.5-7-4.5-7-9V6z", "M9 12l2 2 4-4"], list: ["M5 6h14M5 12h14M5 18h14"], example: ["M4 5h16v11H4zM8 20h8M12 16v4"], steps: ["M5 6h.01M5 12h.01M5 18h.01M9 6h10M9 12h10M9 18h10"],
    add: ["M12 5v14M5 12h14"], edit: ["M4 20h4L19 9l-4-4L4 16z", "M13 7l4 4"], copy: ["M9 9h11v11H9z", "M5 15V5h10"], trash: ["M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13"], board: ["M4 4h5v16H4zM10 4h5v10h-5zM16 4h4v13h-4z"]
  };
})();
