'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-17105557.js');
const core = require('@sankhyalabs/core');
const ezModalContainer = require('@sankhyalabs/ezui/dist/collection/components/ez-modal-container');

const EmailInfoStep = ({ getMessage, changeInfo, data }) => {
  const { format, email } = data || {};
  return index.h("div", null,
    index.h("ez-text-input", Object.assign({ label: `${getMessage("snkExporter.emailSenderInfoStep_sendTo")} (obrigatório) *`, value: email === null || email === void 0 ? void 0 : email.to, onEzChange: evt => { var _a; return changeInfo("to", (_a = evt.detail) === null || _a === void 0 ? void 0 : _a.trim()); } }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("sendTo")}` })),
    index.h("ez-text-input", Object.assign({ label: `${getMessage("snkExporter.emailSenderInfoStep_subject")} (obrigatório) *`, value: email === null || email === void 0 ? void 0 : email.subject, onEzChange: evt => { var _a; return changeInfo("subject", (_a = evt.detail) === null || _a === void 0 ? void 0 : _a.trim()); } }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("subject")}` })),
    index.h("ez-text-area", Object.assign({ label: getMessage("snkExporter.emailSenderInfoStep_message"), value: email === null || email === void 0 ? void 0 : email.message, onEzChange: evt => changeInfo("message", evt.detail) }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("message")}` })),
    ((email === null || email === void 0 ? void 0 : email.attachments) || [])
      .map((file, index$1) => index.h("ez-file-item", Object.assign({ class: "ez-padding-bottom--large", fileName: `${file.name}.${format}`, fileSize: file.size, canRemove: false }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("fileItem_") + (index$1 + 1)}` }))));
};

const OptionsStep = ({ getMessage, changeInfo, data }) => {
  const selectionCount = (data === null || data === void 0 ? void 0 : data.selectedRows) || 0;
  const formatOptions = [
    { label: getMessage("snkExporter.emailSenderOptStep_formatPDF"), value: "pdf" },
    { label: getMessage("snkExporter.emailSenderOptStep_formatXLS"), value: "xls" }
  ];
  const typeOptions = [
    { label: getMessage("snkExporter.emailSenderOptStep_allData"), value: "all" },
    { label: getMessage("snkExporter.emailSenderOptStep_currentPage"), value: "page" },
  ];
  if (selectionCount > 0) {
    const label = getMessage(selectionCount > 1 ? "snkExporter.emailSenderOptStep_someRecords" : "snkExporter.emailSenderOptStep_oneRecord", { SELECTION_COUNT: selectionCount });
    typeOptions.push({
      label,
      value: "selection"
    });
  }
  return index.h("div", null,
    index.h("ez-radio-button", Object.assign({ class: "ez-margin-bottom--large" }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("formato")}` }, { label: getMessage("snkExporter.emailSenderOptStep_lblFormat"), direction: "horizontal", value: data === null || data === void 0 ? void 0 : data.format, onEzChange: evt => changeInfo("format", evt.detail), options: formatOptions })),
    index.h("ez-radio-button", Object.assign({ class: "ez-margin-bottom--large" }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("type")}` }, { label: getMessage("snkExporter.emailSenderOptStep_export"), value: (data === null || data === void 0 ? void 0 : data.type) || "all", onEzChange: evt => changeInfo("type", evt.detail), options: typeOptions })));
};

const SnkExporterEmailSender = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this._stepInfo = [
      {
        subTitle: "snkExporter.emailSenderOptStep_subtitle",
        firstButton: "snkExporter.emailSenderCancelButton",
        secondButton: "snkExporter.emailSenderNextButton",
        firstButtonAction: () => this.close(),
        secondButtonAction: () => this.changeStep(1)
      },
      {
        subTitle: "snkExporter.emailSenderInfoStep_subtitle",
        firstButton: "snkExporter.emailSenderBackButton",
        secondButton: "snkExporter.emailSenderSendButton",
        firstButtonAction: () => this.changeStep(0),
        secondButtonAction: () => this.send(),
        secondButtonEnabledTester: () => this.isFilled(),
        secondButtonClass: "ez-button--primary"
      }
    ];
    this._config = undefined;
    this._opened = false;
    this._currentStep = 0;
    this.getMessage = undefined;
  }
  open(config) {
    var _a;
    this._config = Object.assign(Object.assign({}, config), { format: (_a = config === null || config === void 0 ? void 0 : config.format) !== null && _a !== void 0 ? _a : "pdf" });
    this._opened = true;
    this.changeStep(0);
    return new Promise(accept => {
      this._promiseResolver = accept;
    });
  }
  close() {
    this._opened = false;
    return new Promise(accept => {
      this._promiseResolver = accept;
    });
  }
  getStepMessage(key) {
    const stepInfo = this._stepInfo[this._currentStep] || {};
    return this.getMessage(stepInfo[key]);
  }
  executeButtonAction(first) {
    const stepInfo = this._stepInfo[this._currentStep] || {};
    stepInfo[first ? "firstButtonAction" : "secondButtonAction"]();
  }
  checkButtonEnabled(first) {
    const stepInfo = this._stepInfo[this._currentStep] || {};
    const testFunction = stepInfo[first ? "firstButtonEnabledTester" : "secondButtonEnabledTester"];
    if (testFunction) {
      return testFunction();
    }
    return true;
  }
  changeStep(index) {
    this._currentStep = index;
    if (this._viewStack) {
      this._viewStack.show(this._currentStep);
    }
  }
  send() {
    const { format, type, email, resolver } = this._config;
    resolver({ format, type, email, report: undefined });
  }
  isFilled() {
    var _a, _b;
    return ((_a = this._config.email) === null || _a === void 0 ? void 0 : _a.to) && ((_b = this._config.email) === null || _b === void 0 ? void 0 : _b.subject) ? true : false;
  }
  updateEmailInfo(field, value) {
    this._config = Object.assign(Object.assign({}, this._config), { email: Object.assign(Object.assign({}, this._config.email), { [field]: value }) });
  }
  updateConfigInfo(field, value) {
    this._config = Object.assign(Object.assign({}, this._config), { [field]: value });
  }
  getSecondButtonClass() {
    const stepInfo = this._stepInfo[this._currentStep] || {};
    return stepInfo["secondButtonClass"] || "ez-button--secondary";
  }
  render() {
    core.ElementIDUtils.addIDInfoIfNotExists(this._element, 'snkExporterEmailSender');
    return (index.h("ez-popup", { useHeader: false, size: "x-small", heightMode: "auto", opened: this._opened }, index.h("ez-modal-container", { onEzModalAction: (evt) => {
        if (evt.detail === ezModalContainer.ModalAction.CLOSE)
          this.close();
      }, modalTitle: this.getMessage("snkExporter.emailSenderTitle"), modalSubTitle: this.getStepMessage("subTitle") }, index.h("ez-view-stack", { ref: ref => this._viewStack = ref }, index.h("stack-item", null, index.h(OptionsStep, { getMessage: this.getMessage, data: this._config, changeInfo: (field, value) => this.updateConfigInfo(field, value) })), index.h("stack-item", null, index.h(EmailInfoStep, { getMessage: this.getMessage, data: this._config, changeInfo: (field, value) => this.updateEmailInfo(field, value) }))), index.h("div", { class: "ez-col ez-col--sd-12 ez-flex--justify-end ez-margin-vertical--small" }, index.h("ez-button", Object.assign({ class: "ez-button--tertiary ez-padding-right--medium", label: this.getStepMessage("firstButton"), onClick: () => this.executeButtonAction(true), enabled: this.checkButtonEnabled(true) }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("firstButton")}` })), index.h("ez-button", Object.assign({ class: this.getSecondButtonClass(), label: this.getStepMessage("secondButton"), onClick: () => this.executeButtonAction(false), enabled: this.checkButtonEnabled(false) }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo("secondButton")}` }))))));
  }
  get _element() { return index.getElement(this); }
};

exports.snk_exporter_email_sender = SnkExporterEmailSender;
