import { ApplicationContext } from "@sankhyalabs/core";
import { ActionsType } from "../actions/enum/ActionsType";
const CONFIRMACAO = "__CONFIRMACAO__";
const ESCOLHA_SIM_NAO = "__ESCOLHA_SIMNAO__";
export default class ClientEventConfirm {
  async clientConfirm(clientEvent, recaller) {
    return new Promise((resolve) => {
      var _a, _b;
      const application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
      let actionType = "";
      if (clientEvent.content.event.hasOwnProperty('stpCall')) {
        recaller.requestBody = clientEvent.content.event.stpCall;
        actionType = ActionsType.PROCEDURE;
      }
      else if (clientEvent.content.event.hasOwnProperty('runScript')) {
        recaller.requestBody = clientEvent.content.event.runScript;
        actionType = ActionsType.JAVASCRIPT;
      }
      else if (clientEvent.content.event.hasOwnProperty('javaCall')) {
        recaller.requestBody = clientEvent.content.event.javaCall;
        actionType = ActionsType.JAVA;
      }
      let param = {
        type: 'S',
        sequence: clientEvent.content.event.sequence
      };
      if (!recaller.requestBody.params) {
        recaller.requestBody.params = {
          param: []
        };
      }
      else {
        if (!Array.isArray(recaller.requestBody.params.param)) {
          recaller.requestBody.params.param = [recaller.requestBody.params.param];
        }
      }
      recaller.requestBody.params.param.push(param);
      const title = clientEvent.content.event.title.$;
      const message = clientEvent.content.event.message.$;
      let requestBody;
      switch (actionType) {
        case ActionsType.JAVASCRIPT:
          requestBody = { runScript: recaller.requestBody };
          break;
        case ActionsType.PROCEDURE:
          requestBody = {
            requestBody: {
              stpCall: recaller.requestBody
            }
          };
          break;
        case ActionsType.JAVA:
          requestBody = {
            requestBody: {
              javaCall: recaller.requestBody
            }
          };
          break;
      }
      if (clientEvent.content.event.showNoOption == 'S') {
        param.paramName = ESCOLHA_SIM_NAO;
        const form = document.createElement("snk-client-confirm");
        window.document.body.appendChild(form);
        form.titleMessage = title;
        form.message = message;
        form.accept = async () => {
          param.$ = 'S';
          await recaller.reCall(requestBody);
          resolve();
        };
        form.cancel = async () => {
          param.$ = 'N';
          await recaller.reCall(requestBody);
          resolve();
        };
        form.openPopup();
      }
      else {
        application.confirm(title, message, null, 'warn', {
          labelCancel: (_a = application === null || application === void 0 ? void 0 : application.messagesBuilder) === null || _a === void 0 ? void 0 : _a.getMessage("snkActionsButton.clientEvent.labelCancel"),
          labelConfirm: (_b = application === null || application === void 0 ? void 0 : application.messagesBuilder) === null || _b === void 0 ? void 0 : _b.getMessage("snkActionsButton.clientEvent.labelConfirm")
        }).then(async (confirmResult) => {
          if (confirmResult) {
            param.paramName = CONFIRMACAO;
            param.$ = 'S';
            await recaller.reCall(requestBody);
            resolve();
          }
        });
      }
    });
  }
}
