'use strict';

const React = require('react');
const types = require('../../../components/ui-builder/types.cjs');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }

const React__default = /*#__PURE__*/_interopDefaultCompat(React);

function resolveChildrenVariableReference(children, variables, variableValues) {
  if (types.isVariableReference(children)) {
    const variable = variables.find((v) => v.id === children.__variableRef);
    if (variable) {
      const resolvedValue = variableValues?.[variable.id] ?? variable.defaultValue;
      return String(resolvedValue);
    }
    return "";
  }
  return children;
}
function resolveVariableReferences(props, variables, variableValues) {
  const resolved = {};
  for (const [key, value] of Object.entries(props)) {
    if (types.isVariableReference(value)) {
      const variable = variables.find((v) => v.id === value.__variableRef);
      if (variable) {
        resolved[key] = variableValues?.[variable.id] ?? variable.defaultValue;
      } else {
        resolved[key] = void 0;
      }
    } else if (typeof value === "object" && value !== null && !Array.isArray(value) && !React__default.isValidElement(value)) {
      resolved[key] = resolveVariableReferences(value, variables, variableValues);
    } else {
      resolved[key] = value;
    }
  }
  return resolved;
}

exports.isVariableReference = types.isVariableReference;
exports.resolveChildrenVariableReference = resolveChildrenVariableReference;
exports.resolveVariableReferences = resolveVariableReferences;
