"use strict";

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  var desc = Object.getOwnPropertyDescriptor(m, k);
  if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
    desc = {
      enumerable: true,
      get: function () {
        return m[k];
      }
    };
  }
  Object.defineProperty(o, k2, desc);
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});
var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});
var __importStar = void 0 && (void 0).__importStar || function () {
  var ownKeys = function (o) {
    ownKeys = Object.getOwnPropertyNames || function (o) {
      var ar = [];
      for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
      return ar;
    };
    return ownKeys(o);
  };
  return function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
    __setModuleDefault(result, mod);
    return result;
  };
}();
var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RisingBollardsNode = void 0;
const tree_model_1 = __importDefault(require("tree-model"));
const domain_1 = require("../../domain");
const agv_constant_1 = require("../../entity/agv.constant");
const translation_util_1 = __importStar(require("../../utils/translation.util"));
const risingbollards_axis_util_1 = require("../../utils/risingbollards-axis.util");
const scope_setting_1 = require("../../scope-setting");
const TranslationUtil = translation_util_1.default;
class RisingBollardsNode {
  setTargetHeight(v) {
    this.targetHeight = v;
    return this;
  }
  getTargetHeight() {
    return this.targetHeight;
  }
  setSpeed(v) {
    this.speed = v;
    return this;
  }
  getSpeed() {
    return this.speed;
  }
  setDataModel(dataModel) {
    this.dataModel = dataModel;
    const targetHeight = dataModel.get(this.RisingBollardsTargetHeightKey, this.targetHeight);
    const speed = dataModel.get(this.RisingBollardsSpeedKey, this.speed);
    if (this.isNumber(targetHeight)) {
      this.targetHeight = targetHeight;
    }
    if (this.isNumber(speed)) {
      this.speed = speed;
    }
  }
  getDataModel() {
    this.dataModel.set(this.RisingBollardsTargetHeightKey, this.targetHeight, domain_1.XmlLabelType.FLOAT64);
    this.dataModel.set(this.RisingBollardsSpeedKey, this.speed, domain_1.XmlLabelType.FLOAT64);
    return this.dataModel;
  }
  isNumber(value) {
    return Number.isFinite(value);
  }
  setId(id) {
    this.id = id;
  }
  update(config) {
    let updated = false;
    for (const key in config) {
      const raw = config[key];
      const value = raw && typeof raw === 'object' && 'value' in raw ? raw.value : raw;
      if (key === this.RisingBollardsTargetHeightKey) {
        if (this.isNumber(Number(value)) && Number(value) !== this.getTargetHeight()) {
          this.setTargetHeight(Number(value));
          updated = true;
        }
      } else if (key === this.RisingBollardsSpeedKey) {
        if (this.isNumber(Number(value)) && Number(value) !== this.getSpeed()) {
          this.setSpeed(Number(value));
          updated = true;
        }
      }
    }
    return updated;
  }
  dropNode() {}
  toJSON() {
    const result = {};
    const model = this.getDataModel();
    if (!model || typeof model.getKeys !== 'function') {
      return result;
    }
    model.getKeys().forEach(key => {
      result[key] = {
        value: model.get(key, null),
        hasDefined: !!model.get(key, null)
      };
    });
    return result;
  }
  cloneNode(node) {
    const treeModelInstance = new tree_model_1.default();
    const parseTree = treeModelInstance.parse({});
    const {
      children,
      programNode
    } = node;
    if (programNode) {
      const nodeData = 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;
    return currentNode;
  }
  constructor(programModel) {
    this.RisingBollardsTargetHeightKey = 'rising_bollards_target_height';
    this.RisingBollardsSpeedKey = 'rising_bollards_speed';
    this.id = '';
    this.targetHeight = 1200;
    this.speed = 150;
    this.programModel = programModel;
    this.node = this;
  }
  generateScript(scriptWriter) {
    var _a, _b, _c, _d;
    const tree_node = (_b = (_a = this.programModel) === null || _a === void 0 ? void 0 : _a.getProgramTree().getTreeNodeById(this.id)) === null || _b === void 0 ? void 0 : _b.model;
    if (!tree_node) {
      return;
    }
    const row = tree_node.lineNumber;
    const targetHeight = Number((_c = this.getTargetHeight()) !== null && _c !== void 0 ? _c : 1200);
    const speed = Math.abs(Number((_d = this.getSpeed()) !== null && _d !== void 0 ? _d : 150));
    const axisName = (0, risingbollards_axis_util_1.escapeLuaString)((0, risingbollards_axis_util_1.getRisingBollardsAxisName)());
    scriptWriter.appendLine(`function _aubo_wait_ext_axis_arrival_${row}()`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`local exec_id = getExecId()`);
    scriptWriter.appendLine(`while exec_id == -1 do`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`exec_id = getExecId()`);
    scriptWriter.appendLine(`sleep(0.1)`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`end`);
    scriptWriter.appendLine(`while exec_id ~= -1 do`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`exec_id = getExecId()`);
    scriptWriter.appendLine(`sleep(0.1)`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`end`);
    scriptWriter.end();
    scriptWriter.lineFeed();
    scriptWriter.appendLine(`enableAxisGroup("${agv_constant_1.RISING_BOLLARDS_AXIS_GROUP_NAME}")`);
    scriptWriter.appendLine(`local axis_interface = app.api:getAxisInterface("${axisName}")`);
    scriptWriter.appendLine(`if not axis_interface then`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`error("rising bollards aborted: external axis interface unavailable")`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`end`);
    scriptWriter.appendLine(`textmsg("rising bollards move to group=${agv_constant_1.RISING_BOLLARDS_AXIS_GROUP_NAME}, axis=${axisName}, height=${targetHeight}, speed=${speed}")`);
    scriptWriter.appendLine(`axis_interface:moveExtJoint(${targetHeight}, ${speed}, 1.0, 4)`);
    scriptWriter.appendLine(`_aubo_wait_ext_axis_arrival_${row}()`);
  }
  hasDefined() {
    return true;
  }
  getTitle() {
    const language = (0, scope_setting_1.getCurrentLanguage)();
    return translation_util_1.default.toAimLanguage(language, 'Lift Column', translation_util_1.TranslationNamespace.AGV);
  }
  getIcon(hasDefined) {
    return hasDefined ? `icon${agv_constant_1.PluginsProgramNodeId.AUBO_RISING_BOLLARDS}Complete` : `icon${agv_constant_1.PluginsProgramNodeId.AUBO_RISING_BOLLARDS}`;
  }
}
exports.RisingBollardsNode = RisingBollardsNode;
//# sourceMappingURL=risingbollards-node.js.map