import { ElementIDUtils } from '@sankhyalabs/core';
import { h } from '@stencil/core';
import { ModalAction } from "@sankhyalabs/ezui/dist/collection/components/ez-modal-container";
import { EmailInfoStep } from './email-info-step';
import { OptionsStep } from './options-step';
export class SnkExporterEmailSender {
  constructor() {
    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() {
    ElementIDUtils.addIDInfoIfNotExists(this._element, 'snkExporterEmailSender');
    return (h("ez-popup", { useHeader: false, size: "x-small", heightMode: "auto", opened: this._opened }, h("ez-modal-container", { onEzModalAction: (evt) => {
        if (evt.detail === ModalAction.CLOSE)
          this.close();
      }, modalTitle: this.getMessage("snkExporter.emailSenderTitle"), modalSubTitle: this.getStepMessage("subTitle") }, h("ez-view-stack", { ref: ref => this._viewStack = ref }, h("stack-item", null, h(OptionsStep, { getMessage: this.getMessage, data: this._config, changeInfo: (field, value) => this.updateConfigInfo(field, value) })), h("stack-item", null, h(EmailInfoStep, { getMessage: this.getMessage, data: this._config, changeInfo: (field, value) => this.updateEmailInfo(field, value) }))), h("div", { class: "ez-col ez-col--sd-12 ez-flex--justify-end ez-margin-vertical--small" }, 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) }, { [ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${ElementIDUtils.getInternalIDInfo("firstButton")}` })), h("ez-button", Object.assign({ class: this.getSecondButtonClass(), label: this.getStepMessage("secondButton"), onClick: () => this.executeButtonAction(false), enabled: this.checkButtonEnabled(false) }, { [ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${ElementIDUtils.getInternalIDInfo("secondButton")}` }))))));
  }
  static get is() { return "snk-exporter-email-sender"; }
  static get properties() {
    return {
      "getMessage": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "(key: string, params?: any) => string",
          "resolved": "(key: string, params?: any) => string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        }
      }
    };
  }
  static get states() {
    return {
      "_config": {},
      "_opened": {},
      "_currentStep": {}
    };
  }
  static get methods() {
    return {
      "open": {
        "complexType": {
          "signature": "(config: IExportConfig) => Promise<IExportResult>",
          "parameters": [{
              "tags": [],
              "text": ""
            }],
          "references": {
            "Promise": {
              "location": "global"
            },
            "IExportResult": {
              "location": "import",
              "path": "../data/export-result"
            },
            "IExportConfig": {
              "location": "import",
              "path": "../data/export-config"
            }
          },
          "return": "Promise<IExportResult>"
        },
        "docs": {
          "text": "",
          "tags": []
        }
      },
      "close": {
        "complexType": {
          "signature": "() => Promise<IExportResult>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            },
            "IExportResult": {
              "location": "import",
              "path": "../data/export-result"
            }
          },
          "return": "Promise<IExportResult>"
        },
        "docs": {
          "text": "",
          "tags": []
        }
      }
    };
  }
  static get elementRef() { return "_element"; }
}
