import { r as registerInstance, i as createEvent, h, H as Host, e as getElement } from './index-c04b9ed5.js';
import { A as AssignmentService } from './assignment.service-8d31f141.js';
import { T as TokenService } from './token.service-71a5d33f.js';
import './httpService-7986aeed.js';
import './auth.store-dba2c2da.js';
import './overlays-4233a39d.js';
import './ionic-global-4903e23e.js';
import './hardware-back-button-fa04d6e9.js';
import './helpers-bf0e23d6.js';
import './environment-config.service-b191f276.js';
import './localstorage.service-4bf408c8.js';

const sparkleAssignmentCss = ":host{display:block}.assignment-footer-container{display:flex;justify-content:flex-end}.assignment-footer{display:flex;justify-content:space-between;align-items:center}.error{color:red;margin-right:10px}.success{color:green;margin-right:10px}.uploaded-image{display:block;max-height:140px;width:auto !important}";

const SparkleAssignment = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.formSubmitted = createEvent(this, "formSubmitted", 7);
    this.submitted = true;
    this.assignmentLoading = false;
    this.onSubmit = async (event) => {
      event.preventDefault();
      debugger;
      const formElements = event.currentTarget.querySelectorAll('sparkle-input,sparkle-file-upload');
      const formData = Array.from(formElements).reduce((acc, formElement) => {
        const { name, type, value } = formElement;
        //const name = formElement.getAttribute('name');
        let checked = false;
        if (formElement.querySelector('ion-item')) {
          checked = formElement.querySelector('ion-item').classList.contains('item-checkbox-checked');
        }
        if (name) {
          if (type === 'checkbox') {
            acc[name] = checked;
          }
          else {
            acc[name] = value;
          }
        }
        return acc;
      }, {});
      const requiredFields = Array.from(formElements).filter((formElement) => formElement.required);
      const requiredFieldsFilledIn = requiredFields.every((formElement) => formElement.value !== '');
      if (requiredFieldsFilledIn) {
        this.formSubmitted.emit(formData);
        //call api
      }
      else {
        alert('Please fill in all required fields');
      }
      this.assignmentLoading = true;
      this.errorMessage = '';
      this.successMessage = '';
      var assignmentModel = {
        AssignmentData: JSON.stringify(formData),
        AssignmentName: this.name,
      };
      console.log(assignmentModel);
      try {
        const result = await AssignmentService.getInstance().createAssignment(assignmentModel);
        if (result.Succeeded) {
          this.assignmentLoading = false;
          this.successMessage = 'Assignment submitted Successfully.';
          this.resetForm();
          //reset all inputs
        }
        else {
          this.assignmentLoading = false;
          this.errorMessage = 'There is an error while posting to feed.';
        }
      }
      catch (ex) {
        this.assignmentLoading = false;
      }
    };
  }
  fileUploaded(e) {
    console.log('fileUploaded', e);
  }
  resetForm() {
    const form = this.el.querySelector('form');
    if (form) {
      form.reset();
    }
  }
  renderUserInfo() {
    let authUser = TokenService.getInstance().getAuthUserSync();
    if (authUser && authUser.JWToken) {
      return (h("ion-avatar", { class: "header-avatar", slot: "end" }, authUser.PhotoUrl ? h("img", { src: authUser.PhotoUrl }) : h("img", { src: "/assets/img/user.png" })));
    }
    else {
      return (h("ion-button", { slot: "end", href: "/login" }, "Login to Post"));
    }
  }
  isAuthenticated() {
    return TokenService.getInstance().isAuthenticated();
  }
  render() {
    const userInfo = this.renderUserInfo();
    return (h(Host, null, h("ion-card", null, h("ion-item", { class: "header ion-justify-content-between", lines: "full" }, h("ion-label", null, this.name), userInfo), h("ion-card-content", null, h("form", { onSubmit: this.onSubmit }, h("slot", null), h("div", { class: "assignment-footer-container" }, h("div", { class: "assignment-footer" }, h("div", null, h("div", { class: "error" }, this.errorMessage), h("div", { class: "success" }, this.successMessage)), h("div", null, this.assignmentLoading ? h("ion-spinner", { color: "primary" }) : ''), h("ion-button", { type: "submit", slot: "end", size: "default", disabled: this.assignmentLoading || !this.isAuthenticated() }, "Submit"))))))));
  }
  get el() { return getElement(this); }
};
SparkleAssignment.style = sparkleAssignmentCss;

export { SparkleAssignment as sparkle_assignment };
