import { r as registerInstance, h, e as getElement } from './index-5568e3fa.js';
import { S as SparkleGlobal, G as GetGoalAppData, e as MyGoalTemplate, f as MyGoalTemplateJSON } from './util-6ef753e9.js';
import { E as EnvironmentConfigService } from './environment-config.service-2b5d692b.js';
import { A as AssetsService } from './assets.service-66e848b8.js';
import { A as AppDataService, F as FeedService, g as goalStore, f as feedStore } from './feed.service-33b83cb7.js';
import './auth.store-3ed2389e.js';
import './overlays-34cfa9e0.js';
import './ionic-global-0939c477.js';
import './hardware-back-button-b6ccf74a.js';
import './helpers-39367fe1.js';

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

const SparkleGoalForm = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.name = {
      valid: false,
      value: ''
    };
    this.description = {
      valid: false,
      value: ''
    };
    this.category = {
      valid: false,
      value: ''
    };
    this.progress = 0;
    this.public = true;
    this.goalUpdateText = {
      valid: true,
      value: ''
    };
    this.submitted = false;
  }
  goalHanlder(data) {
    this.updateGoal(data.AppData);
  }
  ;
  componentWillLoad() {
    if (this.appData) {
      this.updateGoal(this.appData.AppData);
    }
  }
  updateGoal(data) {
    if (data) {
      this.name = Object.assign(Object.assign({}, this.name), { value: data.Name });
      this.description = Object.assign(Object.assign({}, this.description), { value: data.Description });
      this.category = Object.assign(Object.assign({}, this.category), { value: data.Category });
      this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { value: data.GoalText });
      this.progress = data.Percentage;
    }
  }
  handleTitle(ev) {
    this.validateTitle();
    this.name = Object.assign(Object.assign({}, this.name), { value: ev.target.value });
  }
  handleDescription(ev) {
    this.validateDescription();
    this.description = Object.assign(Object.assign({}, this.description), { value: ev.target.value });
  }
  handleCategory(ev) {
    this.validateCategoy();
    this.category = Object.assign(Object.assign({}, this.category), { value: ev.target.value });
  }
  handleGoalProgressInfo(ev) {
    this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { value: ev.target.value });
  }
  validateTitle() {
    if (this.name.value && this.name.value.length > 0) {
      this.name = Object.assign(Object.assign({}, this.name), { valid: true });
      return;
    }
    this.name = Object.assign(Object.assign({}, this.name), { valid: false });
  }
  validateDescription() {
    if (this.description.value && this.description.value.length > 0) {
      this.description = Object.assign(Object.assign({}, this.description), { valid: true });
      return;
    }
    this.description = Object.assign(Object.assign({}, this.description), { valid: false });
  }
  validateCategoy() {
    if (this.category.value && this.category.value.length > 0) {
      this.category = Object.assign(Object.assign({}, this.category), { valid: true });
      return;
    }
    this.category = Object.assign(Object.assign({}, this.category), { valid: false });
  }
  validateGoalUpdateText() {
    if (this.goalUpdateText.value && this.goalUpdateText.value.length > 0) {
      this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { valid: true });
      return;
    }
    this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { valid: false });
  }
  async onSubmit(e) {
    e.preventDefault();
    console.log('clicked signup');
    this.validateTitle();
    this.validateDescription();
    this.validateCategoy();
    this.submitted = true;
    if (this.appData && this.appData.Id > 0) {
      this.validateGoalUpdateText();
      if (this.name.valid && this.description.valid && this.category.valid && this.goalUpdateText.valid) {
        //update
        var appData = GetGoalAppData(this.name.value, this.description.value, this.category.value, this.progress, this.goalUpdateText.value);
        const result = await AppDataService.getInstance().updateGoal({ Id: this.appData.Id, MiniAppId: SparkleGlobal.MY_GOALS_APP_ID, AppJsonData: JSON.stringify(appData) });
        if (result.Succeeded) {
          if (this.public) {
            await this.postToFeed();
          }
          else {
            this.dismiss();
          }
        }
      }
    }
    else {
      if (this.name.valid && this.description.valid && this.category.valid) {
        //add
        var appData = GetGoalAppData(this.name.value, this.description.value, this.category.value, this.progress, this.goalUpdateText.value);
        const result = await AppDataService.getInstance().createGoal({ MiniAppId: SparkleGlobal.MY_GOALS_APP_ID, AppJsonData: JSON.stringify(appData) });
        if (result.Succeeded) {
          this.dismiss();
        }
      }
    }
  }
  async postToFeed() {
    const sparkleConfig = EnvironmentConfigService.getInstance().get('sparkle');
    const tocData = await AssetsService.getInstance().loadJsonFile(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) {
      this.dismiss();
    }
  }
  dismiss(data) {
    // dismiss the closest modal and optionally pass back data
    this.el.closest('ion-modal').dismiss({
      'dismissed': true
    });
  }
  renderEditBookingFields() {
    if (this.appData) {
      return (h("div", null, h("ion-item", null, h("ion-label", { position: "stacked", color: "primary" }, "Goal Progress Info"), h("ion-input", { name: "title", type: "text", value: this.goalUpdateText.value, onInput: (ev) => this.handleGoalProgressInfo(ev) })), h("ion-text", { color: "danger" }, h("p", { hidden: this.goalUpdateText.valid || this.submitted === false, class: "ion-padding-start" }, "Goal Progress text is required")), h("ion-item", null, h("ion-label", { position: "stacked" }, "Progress"), h("ion-range", { value: this.progress, min: 1, max: 100, step: 1, snaps: true, pin: true, onIonChange: (e) => this.progress = +e.detail.value }, h("ion-icon", { size: "small", slot: "start", name: "pulse" }), h("ion-icon", { name: "pulse", size: "small" }), h("strong", { slot: "end" }, this.progress))), h("ion-text", { color: "danger" }, h("p", { hidden: this.progress > 0 || this.submitted === false, class: "ion-padding-start" }, "Min Progress value should be 1")), h("ion-item", { lines: "none" }, h("ion-label", null, "Share to Community Wall"), h("ion-checkbox", { slot: "start", onChange: (ev) => this.public = ev.detail.checked, checked: true }))));
    }
  }
  render() {
    return [
      h("div", null, h("ion-toolbar", { color: "primary" }, h("ion-buttons", { slot: "start" }, h("ion-back-button", { defaultHref: "home", onClick: () => this.dismiss() })), h("ion-title", null, "Add New Goal")), h("div", { class: "inner-content" }, h("form", { class: "ion-padding", novalidate: true, onSubmit: (e) => this.onSubmit(e) }, h("ion-list", { "no-lines": true }, h("ion-item", null, h("ion-label", { position: "stacked", color: "primary" }, "Title"), h("ion-input", { name: "title", type: "text", value: this.name.value, onInput: (ev) => this.handleTitle(ev), required: true })), h("ion-text", { color: "danger" }, h("p", { hidden: this.name.valid || this.submitted === false, class: "ion-padding-start" }, "Title is required")), h("ion-item", null, h("ion-label", { position: "stacked", color: "primary" }, "Description"), h("ion-textarea", { name: "desciption", value: this.description.value, onInput: (ev) => this.handleDescription(ev), required: true })), h("ion-text", { color: "danger" }, h("p", { hidden: this.description.valid || this.submitted === false, class: "ion-padding-start" }, "Description is required")), h("ion-item", null, h("ion-label", null, "Category"), h("ion-select", { value: this.category.value, onIonChange: (e) => this.handleCategory(e) }, h("ion-select-option", { value: "Emotional" }, "Emotional"), h("ion-select-option", { value: "Interpersonal" }, "Interpersonal"), h("ion-select-option", { value: "Learning" }, "Learning"), h("ion-select-option", { value: "Financial" }, "Financial"), h("ion-select-option", { value: "Physical" }, "Physical"))), h("ion-text", { color: "danger" }, h("p", { hidden: this.category.valid || this.submitted === false, class: "ion-padding-start" }, "Category is required")), this.renderEditBookingFields()), h("div", { class: "ion-padding" }, this.appData && h("ion-button", { type: "submit", expand: "block", disabled: goalStore.state.goalLoading || feedStore.state.postIdLoading > 0 }, goalStore.state.goalLoading && h("ion-spinner", { name: "dots" }), "  Update"), !this.appData && h("ion-button", { type: "submit", expand: "block", disabled: goalStore.state.goalLoading }, goalStore.state.goalLoading && h("ion-spinner", { name: "dots" }), "  Add")))))
    ];
  }
  get el() { return getElement(this); }
  static get watchers() { return {
    "appData": ["goalHanlder"]
  }; }
};
SparkleGoalForm.style = sparkleGoalFormCss;

export { SparkleGoalForm as sparkle_goal_form };
