// @refresh skip
import { lazy, sharedConfig } from "solid-js";
import { Hydration, NoHydration, getRequestEvent } from "solid-js/web";
// import { IslandManifest } from "./types";
import { splitProps } from "./utils";
export function createIslandReference(Comp, id, name) {
    let Component = Comp;
    if (!import.meta.env.START_ISLANDS) {
        // TODO: have some sane semantics for islands used in non-island mode
        return lazy(Comp);
    }
    function IslandComponent(props) {
        return (<Component {...props}>
        <NoHydration>{props.children}</NoHydration>
      </Component>);
    }
    return ((compProps) => {
        if (import.meta.env.SSR) {
            const context = getRequestEvent();
            const [, props] = splitProps(compProps, ["children"]);
            const [, spreadProps] = splitProps(compProps, []);
            let fpath;
            let styles = [];
            // if (import.meta.env.PROD) {
            //   let x = context.env.manifest?.[path] as IslandManifest;
            //   context.$islands.add(path);
            //   if (x) {
            //     fpath = x.script.href;
            //     styles = x.assets.filter(v => v.type == "style").map(v => v.href);
            //   }
            // } else {
            fpath = id + "#" + name;
            // }
            const serialize = (props) => {
                let offset = 0;
                let el = JSON.stringify(props, (key, value) => {
                    if (value && value.t) {
                        offset++;
                        return undefined;
                    }
                    return value;
                });
                return {
                    "data-props": el,
                    "data-offset": offset
                };
            };
            // @ts-expect-error
            if (!sharedConfig.context?.noHydrate) {
                return <Component {...compProps}/>;
            }
            return (<Hydration>
          <solid-island data-id={fpath} data-when={props["client:idle"] ? "idle" : "load"} data-css={JSON.stringify(styles)} {...serialize(props)}>
            <IslandComponent {...spreadProps}/>
          </solid-island>
        </Hydration>);
        }
        else {
            return <Component {...compProps}/>;
        }
    });
}
