"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_resolved_props_exports = {};
__export(use_resolved_props_exports, {
  useResolvedProps: () => useResolvedProps
});
module.exports = __toCommonJS(use_resolved_props_exports);
var import_react = require("react");
var import_controls = require("@makeswift/controls");
var ReactPage = __toESM(require("../../../state/react-page"));
var import_use_resource_resolver = require("./use-resource-resolver");
var import_use_document_context = require("./use-document-context");
var import_use_selector = require("./use-selector");
var import_use_stylesheet_factory = require("./use-stylesheet-factory");
var import_use_resolvable_record = require("./use-resolvable-record");
var import_prop_error_handling_proxy = require("../utils/prop-error-handling-proxy");
function useControlInstances(elementKey) {
  const documentKey = (0, import_use_document_context.useDocumentKey)();
  return (0, import_use_selector.useSelector)((state) => {
    if (documentKey == null)
      return null;
    return ReactPage.getPropControllers(state, documentKey, elementKey);
  });
}
function useResolvedProps(propDefs, elementData, elementKey) {
  const stylesheetFactory = (0, import_use_stylesheet_factory.useStylesheetFactory)();
  const resourceResolver = (0, import_use_resource_resolver.useResourceResolver)();
  const controls = useControlInstances(elementKey);
  const cache = (0, import_react.useRef)({}).current;
  const resolveProp = (0, import_react.useCallback)(
    (def, propName) => {
      const data = elementData[propName];
      const control = controls?.[propName];
      if (cache[propName] != null && data === cache[propName].data && control === cache[propName].control) {
        return cache[propName].resolvedValue;
      }
      const resolvedValue = def.resolveValue(
        data,
        resourceResolver,
        stylesheetFactory.get(propName),
        control
      );
      cache[propName] = { data, control, resolvedValue };
      return resolvedValue;
    },
    [controls, elementData, resourceResolver, stylesheetFactory]
  );
  const resolvables = (0, import_react.useMemo)(
    () => (0, import_controls.mapValues)(propDefs, (def, propName) => {
      const defaultValue = def.config?.defaultValue;
      return (0, import_prop_error_handling_proxy.propErrorHandlingProxy)(resolveProp(def, propName), defaultValue, (error) => {
        console.warn(
          `Error reading value for prop "${propName}", falling back to \`${defaultValue}\`.`,
          { control: def, error }
        );
      });
    }),
    [propDefs, resolveProp]
  );
  const props = (0, import_use_resolvable_record.useResolvableRecord)(resolvables);
  (0, import_react.useEffect)(() => {
    props.triggerResolve();
  }, [props]);
  const resolvedProps = (0, import_react.useSyncExternalStore)(props.subscribe, props.readStable, props.readStable);
  stylesheetFactory.useDefinedStyles();
  return resolvedProps;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useResolvedProps
});
//# sourceMappingURL=use-resolved-props.js.map