"use strict";

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AgvProgramNodeAdapter = void 0;
const tree_model_1 = __importDefault(require("tree-model"));
const simple_data_model_1 = require("./simple-data-model");
class AgvProgramNodeAdapter {
  constructor(contribution, programModel) {
    this.contribution = contribution;
    this.programModel = programModel;
    this.setDataModel(new simple_data_model_1.SimpleDataModel());
  }
  setId(id) {}
  update(config) {
    var _a, _b;
    const node = this.contribution.getNode();
    const dataModel = this.dataModel;
    if (node === null || node === void 0 ? void 0 : node.setDataModel) {
      const tagMap = new Map();
      for (const key of Object.keys(config !== null && config !== void 0 ? config : {})) {
        const dataKey = key.startsWith("data_") ? key.substring(5) : key;
        const value = config[key];
        (_a = dataModel === null || dataModel === void 0 ? void 0 : dataModel.set) === null || _a === void 0 ? void 0 : _a.call(dataModel, dataKey, value);
        const idx = key.indexOf("_");
        if (idx > 0) {
          const tag = key.substring(0, idx);
          const pureKey = key.substring(idx + 1);
          if (pureKey) {
            tagMap.set(pureKey, tag);
          }
        }
      }
      (_b = dataModel === null || dataModel === void 0 ? void 0 : dataModel.setTagMap) === null || _b === void 0 ? void 0 : _b.call(dataModel, tagMap);
      node.setDataModel(dataModel);
    }
    this.writeNodeToModel();
    return true;
  }
  dropNode() {}
  toJSON() {
    const result = {};
    const model = this.dataModel;
    if (!(model === null || model === void 0 ? void 0 : model.getKeys)) {
      return result;
    }
    model.getKeys().forEach(key => {
      result[key] = {
        value: model.get(key, null),
        hasDefined: this.hasDefined()
      };
    });
    return result;
  }
  getTitle() {
    return this.contribution.getTitle();
  }
  hasDefined() {
    return this.contribution.hasDefined();
  }
  generateScript(scriptWriter) {
    this.contribution.generateScript(scriptWriter);
  }
  setDataModel(dataModel) {
    this.dataModel = this.withSetTagMap(dataModel);
    this.hydrateNodeFromModel();
    this.writeNodeToModel();
  }
  getDataModel() {
    return this.dataModel;
  }
  isTreeModelNode(node) {
    return !!node && typeof node === 'object' && typeof node.addChild === 'function' && typeof node.walk === 'function' && !!node.model;
  }
  cloneNode(node) {
    const treeModelInstance = new tree_model_1.default();
    const parseTree = treeModelInstance.parse({});
    const nodeData = node.programNode.getDataModel();
    this.setDataModel(nodeData);
    const currentNode = this.programModel.getProgramTree().addChild(parseTree, node.nodeId, this, 0);
    currentNode.model.disabled = node.disabled;
    currentNode.model.isBreakpoint = node.isBreakpoint;
    currentNode.model.suppressedComment = node.suppressedComment;
    if (node.children && node.children.length > 0) {
      for (let i = 0; i < node.children.length; i++) {
        const child = node.children[i];
        const service = this.programModel.getProgramNodeFactory().getProgramNodeServiceById(child.nodeId);
        if (!service) {
          throw new Error(`Unsupported child nodeId for clone: ${(child === null || child === void 0 ? void 0 : child.nodeId) || 'unknown'}`);
        }
        const childTree = service.cloneNode(child);
        if (!this.isTreeModelNode(childTree)) {
          throw new Error(`Invalid cloned child. nodeId=${(child === null || child === void 0 ? void 0 : child.nodeId) || 'unknown'}, clonedType=${Object.prototype.toString.call(childTree)}`);
        }
        currentNode.addChild(childTree);
      }
    }
    return currentNode;
  }
  hydrateNodeFromModel() {
    const node = this.contribution.getNode();
    const model = this.dataModel;
    if (!(model === null || model === void 0 ? void 0 : model.getKeys) || !(node === null || node === void 0 ? void 0 : node.setDataModel)) {
      return;
    }
    node.setDataModel(model);
  }
  writeNodeToModel() {
    this.contribution.setModel(this.dataModel);
  }
  withSetTagMap(dataModel) {
    const model = dataModel !== null && dataModel !== void 0 ? dataModel : new simple_data_model_1.SimpleDataModel();
    if (!model.setTagMap) {
      model.setTagMap = () => undefined;
    }
    return model;
  }
}
exports.AgvProgramNodeAdapter = AgvProgramNodeAdapter;
//# sourceMappingURL=program-node-adapter.js.map