import { ah as usePageQueries, bu as useLoopedQuery, bv as useData, bw as isPlainObject } from "./use-page-data.CMUQyNAA.js";
import { u as useBrickManifest } from "./use-brick-manifest.DJ5dVtct.js";
import { g as get } from "./get.DbXiVauf.js";
function useBrickProps({
  brick,
  editable,
  iterationIndex = 0
}) {
  const { props } = brick;
  const pageQueries = usePageQueries();
  const loop = props.loop;
  const loopQuery = useLoopedQuery(loop?.over);
  const manifest = useBrickManifest(brick.type);
  const allData = useData(editable);
  if (!pageQueries.length) {
    return props;
  }
  function replacePlaceholdersForItem(template, item) {
    if (typeof template === "string") {
      return template.replace(/{{(.*?)}}/g, (_, key) => {
        return get(item, key.trim(), "");
      });
    } else if (typeof template === "object" && template !== null) {
      const newValue = {};
      for (const [key, val] of Object.entries(template)) {
        newValue[key] = typeof val === "string" || isPlainObject(val) ? replacePlaceholdersForItem(val, item) : val;
      }
      return newValue;
    }
    return template;
  }
  function replacePlaceholdersGeneric(template) {
    if (typeof template === "string") {
      return template.replace(/{{(.*?)}}/g, (_, key) => {
        const [dataset, variable] = key.trim().split(".");
        if (!dataset || !variable) {
          console.warn("Invalid placeholder:", key);
          return "";
        }
        if (!allData[dataset]) {
          console.warn("No data for dataset:", dataset);
          console.log("Available datasets:", allData);
          return "";
        }
        const data = allData[dataset][iterationIndex] ?? {};
        return get(data, variable, "");
      });
    } else if (typeof template === "object" && template !== null) {
      const newValue = {};
      for (const [key, val] of Object.entries(template)) {
        newValue[key] = typeof val === "string" ? replacePlaceholdersGeneric(val) : val;
      }
      return newValue;
    }
    return template;
  }
  function mapDynamicProps(props2) {
    const mapped = {};
    for (const [key, value] of Object.entries(props2)) {
      const manifestSchema = manifest.props.properties[key];
      if (manifestSchema?.metadata?.consumeQuery && loopQuery) {
        const data = allData[loopQuery.alias] ?? [];
        const template = (Array.isArray(value) ? value : [value])[0];
        mapped[key] = data.map((item) => {
          return replacePlaceholdersForItem(template, { [loopQuery.alias]: item });
        }).slice(0, loop?.overrideLimit ?? void 0);
      } else {
        mapped[key] = typeof value === "string" ? replacePlaceholdersGeneric(value) : value;
      }
    }
    return mapped;
  }
  const dynamicProps = mapDynamicProps(props);
  return dynamicProps;
}
export {
  useBrickProps as u
};
//# sourceMappingURL=use-brick-props.BH1ptgVL.js.map
