"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.BasketUnloadNode = 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 scope_setting_1 = require("../../scope-setting");
const TranslationUtil = translation_util_1.default;
class BasketUnloadNode {
  setStation(v) {
    this.station = v;
    return this;
  }
  getStation() {
    return this.station;
  }
  setDataModel(dataModel) {
    this.dataModel = dataModel;
    const station = dataModel.get(this.BasketUnloadStationKey, this.station);
    if (this.isString(station)) {
      this.station = station;
    }
  }
  getDataModel() {
    const model = this.dataModel;
    model.set(this.BasketUnloadStationKey, this.station, domain_1.XmlLabelType.STRING);
    return model;
  }
  isString(value) {
    return typeof value === 'string';
  }
  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.BasketUnloadStationKey) {
        if (value === null) {
          this.setStation('');
          updated = true;
        } else if (this.isString(value) && value !== this.getStation()) {
          this.setStation(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,
      ...restData
    } = node;
    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.BasketUnloadStationKey = 'unload_station';
    this.id = '';
    this.station = '';
    this.programModel = programModel;
    this.node = this;
  }
  generateScript(scriptWriter) {
    const stationName = this.getStation();
    const stations = agv_constant_1.SingleInstance.getInstance().get(agv_constant_1.ConstantEnum.AGV_STATIONS) || [];
    const stationMap = new Map(stations.map(v => [v.name, v]));
    const target = stationMap.get(stationName);
    if (target && target.pose) {
      scriptWriter.appendLine(`local unload_pose = {x=${target.pose.x}, y=${target.pose.y}, yaw=${target.pose.yaw}}`);
      scriptWriter.appendLine(`local unload_result = agvc.AgvcInterface.relocation(unload_pose, agvc.AgvcInterface.getCurrentMapHeader())`);
      scriptWriter.appendLine(`textmsg("basket unload station relocation result: " .. tostring(unload_result))`);
    } else {
      scriptWriter.appendLine(`textmsg("basket unload station not found: ${stationName || ''}")`);
    }
    scriptWriter.appendLine(`clearNamedVariable("_popup_continue_or_stop")`);
    scriptWriter.appendLine(`do`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`local _popup_t = getControlSystemTime()`);
    scriptWriter.appendLine(`popup(TraceLevel.INFO, "Info", "请完成卸筐后点击继续运行", 1)`);
    scriptWriter.appendLine(`halt()`);
    scriptWriter.appendLine(`while not variableUpdated("_popup_continue_or_stop", _popup_t) do sync() end`);
    scriptWriter.appendLine(`if not getBool("_popup_continue_or_stop", true) then halt() end`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`end`);
  }
  hasDefined() {
    return true;
  }
  getTitle() {
    const language = (0, scope_setting_1.getCurrentLanguage)();
    return translation_util_1.default.toAimLanguage(language, 'Bin Unloading', translation_util_1.TranslationNamespace.AGV);
  }
  getIcon(hasDefined) {
    return hasDefined ? `icon${agv_constant_1.PluginsProgramNodeId.AUBO_BASKET_UNLOAD}Complete` : `icon${agv_constant_1.PluginsProgramNodeId.AUBO_BASKET_UNLOAD}`;
  }
}
exports.BasketUnloadNode = BasketUnloadNode;
//# sourceMappingURL=basketunload-node.js.map