/**
 * `<Drawer.Indent>` — wrapper for the app's main UI that applies
 * `data-active` when any drawer within the nearest `<Drawer.Provider>` is
 * open.
 */

import { dynamic } from "@solidjs/web";
import { createMemo, omit, onCleanup } from "solid-js";
import { composeRefs, userRef } from "../../internals/compose-refs.jsx";
import { mergeRootProps } from "../../internals/merge-root-props.jsx";
import { resolveClassStyle } from "../../internals/resolve-class-style.jsx";
import { useIsoLayoutEffect } from "../../internals/use-iso-layout-effect.jsx";
import { DrawerBackdropCssVars } from "../backdrop/drawer-backdrop-css-vars.jsx";
import { DrawerPopupCssVars } from "../popup/drawer-popup-css-vars.jsx";
import { useDrawerProviderContext } from "../provider/drawer-provider-context.jsx";
/**
 * App-shell wrapper that keys off drawer activity. Renders a `<div>`.
 */
export function DrawerIndent(props) {
  const providerContext = useDrawerProviderContext(true);
  const state = createMemo(() => ({
    active: providerContext?.active() ?? false
  }));
  let indentEl = null;
  const setIndentRef = el => {
    indentEl = el;
  };
  useIsoLayoutEffect(() => providerContext?.visualStateStore, visualStateStore => {
    const el = indentEl;
    if (!el || !visualStateStore) return undefined;
    const syncVisualState = () => {
      const {
        swipeProgress,
        frontmostHeight
      } = visualStateStore.getSnapshot();
      if (swipeProgress <= 0) {
        el.style.setProperty(DrawerBackdropCssVars.swipeProgress, "0");
      } else {
        el.style.setProperty(DrawerBackdropCssVars.swipeProgress, `${swipeProgress}`);
      }
      if (frontmostHeight <= 0) {
        el.style.removeProperty(DrawerPopupCssVars.height);
      } else {
        el.style.setProperty(DrawerPopupCssVars.height, `${frontmostHeight}px`);
      }
    };
    syncVisualState();
    const unsubscribe = visualStateStore.subscribe(syncVisualState);
    return () => {
      unsubscribe();
      el.style.setProperty(DrawerBackdropCssVars.swipeProgress, "0");
      el.style.removeProperty(DrawerPopupCssVars.height);
    };
  });
  onCleanup(() => {
    indentEl = null;
  });
  const internal = {
    get style() {
      return {
        [DrawerBackdropCssVars.swipeProgress]: "0"
      };
    },
    get "data-active"() {
      return state().active ? "" : undefined;
    },
    get "data-inactive"() {
      return state().active ? undefined : "";
    }
  };
  const userClassStyle = resolveClassStyle(props, () => state());
  const userProps = omit(props, "as", "class", "style", "attrs");
  let merged = mergeRootProps(internal, userClassStyle);
  merged = mergeRootProps(merged, userProps);
  const Tag = dynamic(() => props.as ?? "div");
  return <Tag {...merged} ref={composeRefs(setIndentRef, userRef(props))} />;
}
//# sourceMappingURL=drawer-indent.jsx.map
