'use strict';

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

const index = require('./index-51e8292e.js');
const environmentConfig_service = require('./environment-config.service-0e02b049.js');
require('./auth.store-1e630a7d.js');

const sparkleFeedPostCss = ":host{display:block}.post-footer{display:flex;justify-content:space-between}.error{color:red}.success{color:green}.uploaded-image{display:block;max-height:140px;width:auto !important}";

const SparkleFeedPost = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  // @Prop({ connect: 'ion-toast-controller' })
  // toastCtrl: HTMLIonToastControllerElement;
  componentDidLoad() {
    this.postText = this.textValue;
  }
  setText(text) {
    this.postText = text;
  }
  // async postToFeed() {
  //   const sparkleConfig: EnvironmentSparkleConfig = EnvironmentConfigService.getInstance().get('sparkle');
  //   const tocData = await AssetsService.getInstance().loadJsonFile<ICourse>(sparkleConfig.tocPath);
  //   var model = {
  //     CourseId: tocData.id,
  //     MiniAppId: SparkleGlobal.MY_GOALS_APP_ID,
  //     PostText: MyGoalTemplate(this.name.value, this.goalUpdateText.value, this.progress),
  //     PostTextJSON: MyGoalTemplateJSON(this.name.value, this.goalUpdateText.value, this.progress),
  //     IsPublic: this.public,
  //     ExternalResourceUrl: "",
  //   }
  //   const result = await FeedService.getInstance().createPost(model);
  //   if (result.Succeeded) {
  //     //show success toast
  //   }
  // }
  isAuthenticated() {
    try {
      var sparkleConfig = environmentConfig_service.EnvironmentConfigService.getInstance().get('sparkle');
      if (!!sparkleConfig && !!sparkleConfig.auth && !!sparkleConfig.auth.getToken()) {
        return true;
      }
    }
    catch (_a) {
      return false;
    }
  }
  submitPost() {
    this.errorMessage = '';
    this.successMessage = '';
    //let formData = new FormData(this.el.querySelector("form")) as any;
    //var postText = this.el.getElementsByClassName("PostText")[0] as HTMLInputElement;
    this.el.getElementsByClassName('PostExternalUrl')[0];
    this.el.getElementsByClassName('IsPublic')[0];
    // this.dataSvc.saveData(data, '/api/class/feed').subscribe(
    //   x => {
    //     this.successMessage = 'Posted Successfully.';
    //     // this.presentToast('Success');
    //   },
    //   error => {
    //     this.errorMessage = 'There is an error while posting.';
    //     //  this.presentToast('There is an error');
    //   },
    // );
    //TODO can we return data from the dataSvc so we know if it's successful and can update the form? (Perhaps replace it with the post itself?)
    // if (this.assignmentName) {
    //   data.AssignmentData = JSON.stringify({ text: data.PostText, url: data.ExternalResourceUrl });
    //   this.dataSvc.saveData(data, '/api/student/assignment-response').subscribe(
    //     x => {
    //       this.successMessage = 'Posted Successfully.';
    //       // this.presentToast('Success');
    //     },
    //     error => {
    //       this.errorMessage = 'There is an error while posting.';
    //       //  this.presentToast('There is an error');
    //     },
    //   );
    // }
  }
  uploadImage(evt) {
    // this.uploadImageLoading = true;
    // var files = evt.target.files;
    // this.dataSvc.uploadImage(files, '/api/mediaitems/upload').subscribe(
    //   x => {
    //     this.uploadImageLoading = false;
    //     this.uploadedImage = x.length > 0 ? x[0].FullImageUrl : '';
    //     this.externalResourceUrl = x.length > 0 ? x[0].FullImageUrl : '';
    //     //this.successMessage = 'Posted Successfully.'
    //     // this.presentToast('Success');
    //   },
    //   error => {
    //     this.uploadImageLoading = false;
    //     this.errorMessage = 'There is an error while uploading an image.';
    //   },
    // );
  }
  selectFile() {
    var fileInput = document.getElementById('fileInput');
    fileInput.click();
  }
  renderUserInfo() {
    var tokenInfo = { Token: null, ProfileImageUrl: null };
    if (tokenInfo != null && tokenInfo.Token) {
      return (index.h("ion-avatar", { class: "header-avatar", slot: "end" }, index.h("img", { src: "/assets/img/avatar.png" })));
    }
    else {
      return (index.h("ion-button", { slot: "end", href: "/login" }, "Login to Post"));
    }
  }
  render() {
    const userInfo = this.renderUserInfo();
    return (index.h(index.Host, null, index.h("slot", null), index.h("ion-card", null, index.h("ion-item", { class: "header" }, index.h("ion-card-title", null, "Community Wall"), userInfo), index.h("ion-card-content", null, index.h("form", null, index.h("ion-list", null, index.h("ion-item", { lines: "none" }, index.h("ion-label", null, "Public"), index.h("ion-toggle", { class: "IsPublic", slot: "end", color: "primary", name: "IsPublic" })), index.h("ion-item", { class: "padding" }, index.h("ion-textarea", { class: "PostText", required: true, placeholder: "Post Text", name: "PostText", value: this.postText })), index.h("ion-item", { class: "padding" }, index.h("ion-input", { class: "PostExternalUrl", required: true, type: "text", placeholder: "URL", name: "ExternalResourceUrl", value: this.externalResourceUrl })), this.uploadedImage ? (index.h("ion-item", { class: "padding" }, index.h("img", { class: "uploaded-image", src: this.uploadedImage }))) : (''), index.h("ion-item", { lines: "none" }, index.h("input", { type: "file", id: "fileInput", onChange: evt => this.uploadImage(evt), style: { display: 'none' }, accept: "image/*" }), this.uploadImageLoading ? index.h("ion-spinner", { color: "primary" }) : '', index.h("ion-button", { slot: "start", onClick: evt => this.selectFile() }, index.h("ion-icon", { name: "add" }), "Upload Image")), index.h("ion-item", { class: "post-footer", lines: "none" }, index.h("div", null, index.h("div", { class: "error" }, this.errorMessage), index.h("div", { class: "success" }, this.successMessage)), index.h("ion-button", { slot: "end", size: "default", onClick: evt => this.submitPost(), disabled: !this.isAuthenticated() }, "Post"))))))));
  }
  get el() { return index.getElement(this); }
};
SparkleFeedPost.style = sparkleFeedPostCss;

exports.sparkle_feed_post = SparkleFeedPost;
