"use client";

// src/components/shell/product-authoring-context.tsx
import * as React2 from "react";
import {
  isPlatformCreatorWorkspace,
  setRuntimeProductAuthoring
} from "@exxatdesignux/product-framework/product-authoring-gate";
import {
  isProductAuthoringEnabled,
  isProductAuthoringRuntimeAllowed
} from "@exxatdesignux/product-framework/product-authoring";

// src/components/shell/builder-dev-sync.tsx
import * as React from "react";
import { ensureShippedCatalogLoaded } from "@exxatdesignux/product-framework/tenant-products/shipped-catalog";
import {
  isBuilderDevSyncEnvironment,
  startBuilderDevSync
} from "@exxatdesignux/product-framework/tenant-products/builder-dev-sync";
function BuilderDevSync() {
  const authoring = useProductAuthoringEnabled();
  React.useEffect(() => {
    if (!authoring || !isBuilderDevSyncEnvironment()) return;
    let stop = () => {
    };
    void ensureShippedCatalogLoaded().finally(() => {
      stop = startBuilderDevSync();
    });
    return () => stop();
  }, [authoring]);
  return null;
}

// src/components/shell/product-authoring-context.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var ProductAuthoringContext = React2.createContext(
  void 0
);
function ProductAuthoringProvider({
  enabled,
  children
}) {
  const resolved = React2.useMemo(
    () => resolveProductAuthoring(enabled),
    [enabled]
  );
  React2.useEffect(() => {
    setRuntimeProductAuthoring(enabled === void 0 ? null : enabled);
    return () => setRuntimeProductAuthoring(null);
  }, [enabled]);
  return /* @__PURE__ */ jsxs(ProductAuthoringContext.Provider, { value: resolved, children: [
    /* @__PURE__ */ jsx(BuilderDevSync, {}),
    children
  ] });
}
function resolveProductAuthoring(explicit) {
  if (explicit !== void 0) return explicit;
  if (!isProductAuthoringRuntimeAllowed()) return false;
  return isProductAuthoringEnabled();
}
function useProductAuthoringEnabled() {
  const ctx = React2.useContext(ProductAuthoringContext);
  if (ctx !== void 0) return ctx;
  if (!isProductAuthoringRuntimeAllowed()) return false;
  return isProductAuthoringEnabled();
}
function getProductAuthoringRole(authoringEnabled) {
  if (isPlatformCreatorWorkspace()) return "platform-creator";
  if (authoringEnabled) return "builder";
  return "end-user";
}
export {
  ProductAuthoringProvider,
  getProductAuthoringRole,
  resolveProductAuthoring,
  useProductAuthoringEnabled
};
//# sourceMappingURL=product-authoring-context.js.map