"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OpenLoopNodeService = void 0;
const webscope_service_1 = require("../../webscope-service");
const openloop_node_config_1 = require("./openloop-node-config");
const openloop_node_1 = require("./openloop-node");
let openLoopVisionResultNextIndex = 1;
let openLoopVisionResultSeedInitialized = false;
class OpenLoopNodeService {
  constructor() {
    this.programNodeConfig = new openloop_node_config_1.OpenLoopNodeConfig();
    this.funcMap = new Map();
    this.funcMap.set('openloop/visual/add-variable', this.addVisualLocalizationVariable.bind(this));
  }
  loadFromXml(dataModel) {
    const node = new openloop_node_1.OpenLoopNode(this.programModel);
    node.setDataModel(dataModel);
    return node;
  }
  setProgramModel(programModel) {
    this.programModel = programModel;
  }
  getProgramNodeConfig() {
    return this.programNodeConfig;
  }
  getRpcMap() {
    return this.funcMap;
  }
  createNode() {
    const node = new openloop_node_1.OpenLoopNode(this.programModel);
    this.initializeNodeDefaults(node);
    return {
      nodeId: this.programNodeConfig.id,
      node,
      children: []
    };
  }
  cloneNode(node) {
    return new openloop_node_1.OpenLoopNode(this.programModel).cloneNode(node);
  }
  initOpenLoopVisionResultSeed(variableModel) {
    if (openLoopVisionResultSeedInitialized) {
      return;
    }
    const prefix = openloop_node_1.OPENLOOP_VISION_RECOGNITION_SUCCESS_SUFFIX;
    const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const escapedPrefixNoUnderscore = prefix.replace(/_+$/, '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const namePattern = new RegExp(`^(?:${escapedPrefix}|${escapedPrefixNoUnderscore})(\\d+)$`);
    let maxIndex = 0;
    try {
      const all = typeof (variableModel === null || variableModel === void 0 ? void 0 : variableModel.getAll) === 'function' ? variableModel.getAll() : [];
      for (const variable of all || []) {
        const rawName = variable === null || variable === void 0 ? void 0 : variable.name;
        if (typeof rawName !== 'string') {
          continue;
        }
        const matched = rawName.match(namePattern);
        if (!matched) {
          continue;
        }
        const index = Number(matched[1]);
        if (Number.isFinite(index) && index > maxIndex) {
          maxIndex = index;
        }
      }
    } catch (error) {}
    openLoopVisionResultNextIndex = maxIndex + 1;
    openLoopVisionResultSeedInitialized = true;
  }
  getNextOpenLoopVariableName(variableModel) {
    this.initOpenLoopVisionResultSeed(variableModel);
    const next = `${openloop_node_1.OPENLOOP_VISION_RECOGNITION_SUCCESS_SUFFIX}${openLoopVisionResultNextIndex}`;
    openLoopVisionResultNextIndex += 1;
    return next;
  }
  createVisualLocalizationVariable() {
    const webscopeService = (0, webscope_service_1.getWebscopeService)();
    if (!webscopeService) {
      throw new Error('Webscope service is not ready');
    }
    const variableModel = webscopeService.getVariableModel();
    const expression = webscopeService.getValueFactory().createExpression();
    expression.append('false');
    const expectedName = this.getNextOpenLoopVariableName(variableModel);
    const variable = webscopeService.getVariableModel().getVariableFactory().createGlobalVariable(expectedName, expression);
    if (variable && variable.name !== expectedName) {
      variable.name = expectedName;
    }
    variableModel.addVariable(variable);
    return variable;
  }
  addVisualLocalizationVariable() {
    const variable = this.createVisualLocalizationVariable();
    return variable.name;
  }
  initializeNodeDefaults(node) {
    const webscopeService = (0, webscope_service_1.getWebscopeService)();
    if (!webscopeService) {
      return;
    }
    try {
      const variable = this.createVisualLocalizationVariable();
      if (variable) {
        node.setVisionResult(variable);
      }
    } catch (error) {}
  }
}
exports.OpenLoopNodeService = OpenLoopNodeService;
//# sourceMappingURL=openloop-node-service.js.map