'use strict';

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

const index = require('./index-51e8292e.js');
const assignment_service = require('./assignment.service-690fb59c.js');
const token_service = require('./token.service-7a41953d.js');
require('./httpService-d2e7bf8f.js');
require('./auth.store-1e630a7d.js');
require('./overlays-32df265a.js');
require('./ionic-global-2cde9d3a.js');
require('./hardware-back-button-92d97ff8.js');
require('./helpers-c2496722.js');
require('./environment-config.service-c8ff1f3d.js');
require('./localstorage.service-6346a41d.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) {
    index.registerInstance(this, hostRef);
    this.formSubmitted = index.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 assignment_service.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 = token_service.TokenService.getInstance().getAuthUserSync();
    if (authUser && authUser.JWToken) {
      return (index.h("ion-avatar", { class: "header-avatar", slot: "end" }, authUser.PhotoUrl ? index.h("img", { src: authUser.PhotoUrl }) : index.h("img", { src: "/assets/img/user.png" })));
    }
    else {
      return (index.h("ion-button", { slot: "end", href: "/login" }, "Login to Post"));
    }
  }
  isAuthenticated() {
    return token_service.TokenService.getInstance().isAuthenticated();
  }
  render() {
    const userInfo = this.renderUserInfo();
    return (index.h(index.Host, null, index.h("ion-card", null, index.h("ion-item", { class: "header ion-justify-content-between", lines: "full" }, index.h("ion-label", null, this.name), userInfo), index.h("ion-card-content", null, index.h("form", { onSubmit: this.onSubmit }, index.h("slot", null), index.h("div", { class: "assignment-footer-container" }, index.h("div", { class: "assignment-footer" }, index.h("div", null, index.h("div", { class: "error" }, this.errorMessage), index.h("div", { class: "success" }, this.successMessage)), index.h("div", null, this.assignmentLoading ? index.h("ion-spinner", { color: "primary" }) : ''), index.h("ion-button", { type: "submit", slot: "end", size: "default", disabled: this.assignmentLoading || !this.isAuthenticated() }, "Submit"))))))));
  }
  get el() { return index.getElement(this); }
};
SparkleAssignment.style = sparkleAssignmentCss;

exports.sparkle_assignment = SparkleAssignment;
