/*
  Copyright 2020-2026 Lowdefy, Inc

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

import React, { useEffect, useState } from 'react';

import DevStreamContext from './DevStreamContext.js';
import useMutateCache from '../lib/client/utils/useMutateCache.js';
import waitForRestartedServer from '../lib/client/utils/waitForRestartedServer.js';

// SSE listener — config rebuilds notify via /api/reload. Tailwind CSS updates
// arrive through Vite HMR now (globals.css is in the dev module graph), so
// the old tailwind-jit.css link cache-bust is gone.
//
// This is the tab's only EventSource. It is shared through DevStreamContext
// so Inspector.jsx can listen on the same connection instead of opening a
// second one — see the connection-limit note in DevStreamContext.js.
const Reload = ({ children, basePath, lowdefy }) => {
  const [reset, setReset] = useState(false);
  const [restarting, setRestarting] = useState(false);
  const [devStream, setDevStream] = useState({ source: null, tabId: null });
  // reset is a one-shot boolean the engine lowers only after a page mounts
  // (getContext.js) — while a page is suspended it stays true and setReset(true)
  // bails out of re-rendering. The tick always changes, so every reload event
  // re-invokes Routing's render prop, which re-reads reloadVersion and mints a
  // fresh Suspense/SWR key — a suspended tab recovers instead of stranding on
  // the building screen.
  const [, setReloadTick] = useState(0);
  const mutateCache = useMutateCache(basePath);
  useEffect(() => {
    const sse = new EventSource(`${basePath}/api/reload`);

    // routes/reload.js registers every connection as an inspectable tab and
    // announces the id it chose as the first event on the stream. The stream
    // is published to consumers only then, in one state update, so the page
    // subtree re-renders once at mount rather than once per field.
    sse.addEventListener('tab', (message) => {
      const { tabId } = JSON.parse(message.data);
      setDevStream({ source: sse, tabId });
    });

    sse.addEventListener('reload', () => {
      // add a update delay to prevent rerender before server is shut down for rebuild, ideally we don't want to do this.
      // TODO: We need to pass a flag when a rebuild will happen so that client does not trigger render.
      setTimeout(async () => {
        await mutateCache();
        if (lowdefy._internal?.initialised) {
          lowdefy._internal.initialised = false;
        }
        setReset(true);
        setReloadTick((tick) => tick + 1);
        console.log('Reloaded config.');
      }, 600);
    });

    sse.onerror = () => {
      setRestarting(true);
      console.log('Rebuilding Lowdefy App.');
      sse.close();
      waitForRestartedServer(basePath);
    };
    return () => {
      sse.close();
    };
  }, []);
  return (
    <DevStreamContext.Provider value={devStream}>
      {children({ reset, setReset, restarting })}
    </DevStreamContext.Provider>
  );
};

export default Reload;
