import { createMemo } from "solid-js";
import { mergeElementStyles } from "./merge-root-props.jsx";

/**
 * Helper for the wave-3 `as=`/`dynamic()` element migration: resolve `class` / `style`
 * from a Base UI component's `props` against the live state accessor and
 * return a plain object with reactive getters suitable for `mergeRootProps`.
 *
 * Both keys may be raw values OR functions of state — the functions are
 * called inside the getter so they re-run on every reactive dependency
 * change. Returning `undefined` is fine; `mergeRootProps` treats undefined
 * as "absent" so internal defaults still win.
 */

function composeClasses(legacy, attrs) {
  if (legacy === undefined || legacy === false) return attrs;
  if (attrs === undefined || attrs === false) return legacy;
  return [legacy, attrs];
}
export function resolveClassStyle(props, getState) {
  // Resolve all state-aware styling inputs in one computation. Reading any
  // output observes the same state snapshot and does not independently invoke
  // class/style/attrs callbacks.
  const resolved = createMemo(() => {
    const state = getState();
    const classValue = props.class;
    const styleValue = props.style;
    const attrsValue = props.attrs;
    const legacyClass = typeof classValue === "function" ? classValue(state) : classValue;
    const legacyStyle = typeof styleValue === "function" ? styleValue(state) : styleValue;
    const attrs = typeof attrsValue === "function" ? attrsValue(state) : attrsValue;
    return {
      class: composeClasses(legacyClass, attrs?.class),
      style: mergeElementStyles(legacyStyle, attrs?.style),
      "data-style-src": attrs?.["data-style-src"]
    };
  });
  return {
    get class() {
      return resolved().class;
    },
    get style() {
      return resolved().style;
    },
    get "data-style-src"() {
      return resolved()["data-style-src"];
    }
  };
}
//# sourceMappingURL=resolve-class-style.jsx.map
