// packages/components/src/slot-fill/provider.tsx
import { useState } from "@wordpress/element";
import SlotFillContext from "./context.js";
import { observableMap } from "@wordpress/compose";
import { jsx as _jsx } from "react/jsx-runtime";
function createSlotRegistry() {
  const slots = observableMap();
  const fills = observableMap();
  function registerSlot(name, instance) {
    slots.set(name, instance);
  }
  function unregisterSlot(name, instance) {
    if (slots.get(name) !== instance) {
      return;
    }
    slots.delete(name);
  }
  function registerFill(name, instance, children) {
    fills.set(name, [...fills.get(name) || [], {
      instance,
      children
    }]);
  }
  function unregisterFill(name, instance) {
    const fillsForName = fills.get(name);
    if (!fillsForName) {
      return;
    }
    fills.set(name, fillsForName.filter((fill) => fill.instance !== instance));
  }
  function updateFill(name, instance, children) {
    const fillsForName = fills.get(name);
    if (!fillsForName) {
      return;
    }
    const fillForInstance = fillsForName.find((f) => f.instance === instance);
    if (!fillForInstance) {
      return;
    }
    if (fillForInstance.children === children) {
      return;
    }
    fills.set(name, fillsForName.map((f) => {
      if (f.instance === instance) {
        return {
          instance,
          children
        };
      }
      return f;
    }));
  }
  return {
    slots,
    fills,
    registerSlot,
    unregisterSlot,
    registerFill,
    unregisterFill,
    updateFill
  };
}
function SlotFillProvider({
  children
}) {
  const [contextValue] = useState(createSlotRegistry);
  return /* @__PURE__ */ _jsx(SlotFillContext.Provider, {
    value: contextValue,
    children
  });
}
var provider_default = SlotFillProvider;
export {
  SlotFillProvider,
  provider_default as default
};
//# sourceMappingURL=provider.js.map
