"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 merge_exports = {};
__export(merge_exports, {
  mergeElementTreeTranslatedData: () => mergeElementTreeTranslatedData,
  mergeTranslatedData: () => mergeTranslatedData
});
module.exports = __toCommonJS(merge_exports);
var import_react_page = require("../react-page");
var Documents = __toESM(require("../modules/read-only-documents"));
var import_prop_controllers = require("@makeswift/prop-controllers");
var import_merge_translations_visitor = require("../../controls/visitors/merge-translations-visitor");
var import_descriptors = require("../../prop-controllers/descriptors");
function mergeElementTreeTranslatedData(state, elementTree, translatedData) {
  function merge(state2, translatedData2) {
    return function(node) {
      if (Documents.isElementReference(node))
        return node;
      const elementDescriptors = (0, import_react_page.getPropControllerDescriptors)(state2);
      const descriptors = elementDescriptors.get(node.type);
      if (descriptors == null) {
        throw new Error(`Can't merge element of type "${node.type}" because it has no descriptors`);
      }
      const context = {
        translatedData: translatedData2,
        mergeTranslatedData: merge(state2, translatedData2)
      };
      const props = {};
      for (const propName of Object.keys(descriptors)) {
        const descriptor = descriptors[propName];
        props[propName] = mergeTranslatedData(
          descriptor,
          node.props[propName],
          translatedData2[`${node.key}:${propName}`],
          context
        );
      }
      return { ...node, props };
    };
  }
  return merge(state, translatedData)(elementTree);
}
function mergeTranslatedData(definition, data, translatedData, context) {
  if (data == null)
    return data;
  if (!(0, import_descriptors.isLegacyDescriptor)(definition)) {
    const mergeTranslationsVisitor = new import_merge_translations_visitor.ReactMergeTranslationsVisitor(context);
    return definition.accept(mergeTranslationsVisitor, data, translatedData);
  }
  switch (definition.type) {
    case import_prop_controllers.Types.TextInput:
    case import_prop_controllers.Types.TextArea:
      if (translatedData == null)
        return data;
      return translatedData;
    case import_prop_controllers.Types.Grid:
      return (0, import_prop_controllers.mergeGridPropControllerTranslatedData)(data, context);
    default:
      return data;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  mergeElementTreeTranslatedData,
  mergeTranslatedData
});
//# sourceMappingURL=merge.js.map