import { h, r as registerInstance, H as Host, e as getElement } from './index-c04b9ed5.js';
import { t as toastController } from './overlays-4233a39d.js';
import { F as FacilitatorService, f as facilitatorStore } from './facilitator.service-18c47bdf.js';
import './ionic-global-4903e23e.js';
import './hardware-back-button-fa04d6e9.js';
import './helpers-bf0e23d6.js';
import './httpService-7986aeed.js';
import './auth.store-dba2c2da.js';
import './environment-config.service-b191f276.js';
import './token.service-71a5d33f.js';
import './localstorage.service-4bf408c8.js';

const LoadingSpinner = () => (h("div", { class: "spinner-border", role: "status" }));

const facilitatorNotesFormCss = ":host{display:block}";

const FacilitatorNotesForm = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.formControls = {
      noteText: {
        isValid: true,
        value: "",
        errorMessage: "",
        validate: value => {
          console.log(value);
          if (value && value.length > 0) {
            return { valid: true, errorMessage: "" };
          }
          else {
            return { valid: false, errorMessage: "Notes is required" };
          }
        },
      }
    };
    this.submitted = false;
  }
  componentDidLoad() {
    if (this.initialValues) {
      this.changeFormValue("noteText", this.initialValues.NoteText);
    }
  }
  async onSubmit(e) {
    e.preventDefault();
    this.submitted = true;
    let isFormValid = true;
    //Run all validation functions
    for (let controlName in this.formControls) {
      let control = this.formControls[controlName];
      control.isValid = control.validate(control.value).valid;
      control.errorMessage = control.validate(control.value).errorMessage;
      if (!control.isValid) {
        isFormValid = false;
      }
    }
    if (isFormValid) {
      if (this.initialValues && this.initialValues.Id > 0) {
        let result = await FacilitatorService.getInstance().updateFacilitatorNote({
          NoteText: this.formControls.noteText.value,
          Id: this.initialValues.Id
        });
        if (result.Succeeded) {
          const toast = await toastController.create({
            message: "Notes updated successfully",
            duration: 3000
          });
          toast.present();
          this.dismiss();
        }
        else {
          const toast = await toastController.create({
            message: result.Message,
            duration: 3000
          });
          toast.present();
        }
      }
      else {
        let result = await FacilitatorService.getInstance().addFacilitatorNote({
          NoteText: this.formControls.noteText.value
        });
        if (result.Succeeded) {
          const toast = await toastController.create({
            message: "Notes added successfully",
            duration: 3000
          });
          toast.present();
          this.dismiss();
        }
        else {
          const toast = await toastController.create({
            message: result.Message,
            duration: 3000
          });
          toast.present();
        }
      }
    }
  }
  dismiss(data) {
    // dismiss the closest modal and optionally pass back data
    this.el.closest('ion-modal').dismiss({
      'dismissed': true
    });
  }
  changeFormValue(controlName, value) {
    this.formControls = Object.assign(Object.assign({}, this.formControls), { [controlName]: Object.assign(Object.assign({}, this.formControls[controlName]), { value: value, isValid: this.formControls[controlName].validate(value).valid, errorMessage: this.formControls[controlName].validate(value).errorMessage }) });
  }
  render() {
    const noteTextValidation = !this.formControls.noteText.isValid ? "is-invalid" : "is-valid";
    return (h(Host, null, h("ion-header", { translucent: true }, h("ion-toolbar", null, h("ion-title", null, "Facilitator Note"), h("ion-buttons", { slot: "end" }, h("ion-button", { onClick: () => this.dismiss() }, "Close")))), h("ion-content", { class: "ion-padding" }, h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true }, h("div", { class: "mb-3" }, h("label", { htmlFor: "validationTextarea", class: "form-label" }, "Notes"), h("textarea", { class: `form-control ${this.submitted && noteTextValidation}`, placeholder: "Notes", value: this.formControls.noteText.value, onChange: (e) => this.changeFormValue("noteText", e.target.value) }), this.formControls.noteText.errorMessage && h("div", { class: "invalid-feedback" }, this.formControls.noteText.errorMessage)), h("div", { class: "mb-3" }, facilitatorStore.state.notesLoading && h(LoadingSpinner, null), h("button", { class: "btn btn-primary", type: "submit", disabled: facilitatorStore.state.notesLoading }, "Submit"))))));
  }
  get el() { return getElement(this); }
};
FacilitatorNotesForm.style = facilitatorNotesFormCss;

export { FacilitatorNotesForm as sparkle_facilitator_notes_form };
