'use strict';

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

const index = require('./index-ee190ee0.js');
const isAdmin = require('./isAdmin-7ca1c1ef.js');
const store = require('./store-27073b3b.js');
require('./index-4901314f.js');
require('./getHandlebarsHelpers-8ef28401.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./localforage-af1b8c0e.js');

const modalAddStepCss = "floodteam-modal-add-step #step-type span{display:flex;justify-content:center;align-items:center;padding:1rem;flex-direction:column;margin:1rem auto;border:2px solid var(--step-type-color, var(--ion-color-medium));border-radius:10px}floodteam-modal-add-step #step-type span:hover{cursor:pointer;--step-type-color:var(--ion-color-primary)}floodteam-modal-add-step #step-type span.active{--step-type-color:var(--ion-color-primary)}floodteam-modal-add-step #step-type ion-icon{display:block;margin:0 auto;height:60px;width:60px;color:var(--step-type-color, var(--ion-text-medium));opacity:0.6}floodteam-modal-add-step #step-type span.active ion-icon{opacity:1}floodteam-modal-add-step #step-type label{display:block;margin:0 auto;text-align:center;font-size:1.1rem;color:var(--step-type-color, var(--ion-text-medium))}";

const ModalAddStep = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinSubmit = index.createEvent(this, "fireenjinSubmit", 7);
    this.floodteamCloseModal = index.createEvent(this, "floodteamCloseModal", 7);
    this.floodteamOpenModal = index.createEvent(this, "floodteamOpenModal", 7);
    this.stepInput = {};
    this.toggleSteps = false;
    this.showFields = true;
    this.preview = false;
    this.type = undefined;
    this.steps = [];
    this.templates = undefined;
    this.stepId = undefined;
    this.step = undefined;
    this.field = undefined;
    this.isAdvancedOpen = false;
    this.tab = undefined;
    this.db = undefined;
  }
  onSubmit(event) {
    var _a, _b, _c;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "addFieldForm") {
      this.field = ((_c = (_b = event.detail) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.field) || null;
      if (this.field) {
        this.addFieldToStep(this.field);
      }
      setTimeout(() => {
        this.stepFormEl.submit();
      }, 100);
      this.fieldEl.value = "";
    }
  }
  saveStep() {
    this.stepFormEl.submit();
    this.floodteamCloseModal.emit();
  }
  onCodeChange(event) {
    this.step[event.target.name] = event.target.value;
    this.step = { ...this.step };
  }
  toggleFieldsVisibility() {
    this.showFields = !this.showFields;
  }
  stepSelect(step) {
    this.preview = false;
    this.stepId = step.id;
    this.step = step;
    this.tabsEl.select("addStep");
    this.toggleFieldsVisibility();
    setTimeout(() => {
      this.showFields = true;
    }, 100);
  }
  previewStep(step) {
    this.stepPreview = [step];
    this.preview = true;
    setTimeout(() => {
      this.tabsEl.select("stepPreview");
    }, 100);
  }
  fieldSelect(field) {
    this.field = field;
    this.toggleFieldsVisibility();
  }
  addFieldToStep(field) {
    if (!this.step.fields)
      this.step.fields = [];
    this.step.fields.splice(0, 1, typeof field === "string" ? JSON.parse(field) : field);
    this.step = { ...this.step };
    this.showFields = false;
  }
  deleteField(event, index) {
    event.preventDefault();
    event.stopPropagation();
    if (!confirm("Are you sure you want to delete this field?"))
      return;
    (this.step.fields || []).splice(index, 1);
    this.stepFormEl.submit();
    this.step = { ...this.step };
  }
  deleteStep(event, step) {
    event.preventDefault();
    event.stopPropagation();
    if (!confirm(`Are you sure you want to delete ${step === null || step === void 0 ? void 0 : step.name}?`))
      return;
    this.fireenjinSubmit.emit({
      endpoint: "deleteStep",
      data: {
        id: step === null || step === void 0 ? void 0 : step.id,
      },
    });
  }
  componentDidLoad() {
    this.tab = this.step || this.stepId ? "addStep" : "steps";
    this.tabsEl.select(this.tab);
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    return [
      index.h("ion-header", null, index.h("ion-toolbar", null, index.h("ion-title", null, "Modal Step"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { color: "primary", onClick: () => this.saveStep() }, index.h("ion-icon", { slot: "icon-only", name: "save" })), index.h("ion-button", { onClick: () => this.floodteamCloseModal.emit() }, index.h("ion-icon", { slot: "icon-only", name: "close-circle-outline" }))))),
      index.h("ion-content", null, index.h("ion-tabs", { ref: (el) => (this.tabsEl = el) }, index.h("ion-tab-bar", { slot: "top" }, index.h("ion-tab-button", { tab: "addStep" }, index.h("ion-icon", { name: "create-outline" }), index.h("ion-label", null, "Add/Edit Step")), index.h("ion-tab-button", { tab: "steps" }, index.h("ion-icon", { name: "documents-outline" }), index.h("ion-label", null, "Steps")), index.h("ion-tab-button", { onClick: () => this.previewStep(this.step), tab: "stepPreview" }, index.h("ion-icon", { name: "eye" }), index.h("ion-label", null, "Preview"))), index.h("ion-tab", { tab: "addStep" }, index.h("ion-content", null, index.h("fireenjin-form", { hideControls: true, "data-target": "ion-app", formData: this.step, ref: (el) => (this.stepFormEl = el), endpoint: !this.stepId ? "addStep" : "editStep", documentId: this.stepId, disableLoader: true, beforeSubmit: async (data) => ({
          ...data,
          ...this.stepInput,
        }) }, index.h("ion-card", null, index.h("ion-card-header", null, index.h("ion-card-title", null, this.stepId ? "Edit Step" : "Add Step")), index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", { size: "12" }, index.h("fireenjin-input", { required: true, name: "name", label: "Name", labelPosition: "stacked", value: (_a = this.step) === null || _a === void 0 ? void 0 : _a.name }))), index.h("ion-row", { id: "step-type" }, index.h("ion-col", { size: "4" }, index.h("span", { class: {
          active: this.type === "template",
        }, onClick: () => (this.type = "template") }, index.h("ion-icon", { name: "create" }), index.h("label", null, "Template"))), index.h("ion-col", { size: "4" }, index.h("span", { class: {
          active: this.type === "fields",
        }, onClick: () => (this.type = "fields") }, index.h("ion-icon", { name: "clipboard" }), index.h("label", null, "Fields"))), index.h("ion-col", { size: "4" }, index.h("span", { class: {
          active: this.type === "component",
        }, onClick: () => (this.type = "component") }, index.h("ion-icon", { name: "cube" }), index.h("label", null, "Component"))))), index.h("ion-accordion-group", { value: this.isAdvancedOpen ? "advanced" : null, onIonChange: (event) => (this.isAdvancedOpen = event.target.value === "advanced") }, index.h("ion-accordion", { value: "advanced" }, index.h("ion-item", { slot: "header" }, index.h("ion-label", null, "Advanced")), index.h("ion-grid", { slot: "content" }, index.h("ion-row", null, index.h("ion-col", { size: "12" }, index.h("ion-item", { class: "json-editor-field", style: { zIndex: "3" } }, index.h("ion-label", null, index.h("h2", null, "Before Html"), this.isAdvancedOpen && (index.h("fireenjin-code-editor", { ref: (el) => (this.beforeEl = el), autoExpand: true, language: "html", name: "beforeHTML", value: (_b = this.step) === null || _b === void 0 ? void 0 : _b.beforeHTML })))), index.h("ion-item", { class: "json-editor-field", style: { zIndex: "3" } }, index.h("ion-label", null, index.h("h2", null, "After Html"), this.isAdvancedOpen && (index.h("fireenjin-code-editor", { ref: (el) => (this.afterEl = el), autoExpand: true, language: "html", name: "afterHTML", value: (_c = this.step) === null || _c === void 0 ? void 0 : _c.afterHTML })))), index.h("fireenjin-select-tags", { ref: (el) => (this.tagsEl = el), name: "tags", labelPosition: "stacked", label: "Tags", value: (_d = this.step) === null || _d === void 0 ? void 0 : _d.tags, allowAdding: true, options: [
          {
            label: "Important",
            value: "important",
          },
        ] }))))))), this.type === "template" && (index.h("ion-card", null, index.h("ion-card-header", null, index.h("ion-card-title", null, "Template")), index.h("floodteam-select-template", { templates: this.templates || [], name: "templateId", label: "Step Template", value: (_f = (_e = this.step) === null || _e === void 0 ? void 0 : _e.template) === null || _f === void 0 ? void 0 : _f.id }))), this.type === "component" && (index.h("ion-card", null, index.h("ion-card-header", null, index.h("ion-card-title", null, "Component")), index.h("ion-card-content", null, index.h("fireenjin-input", { name: "component", label: "Tag Name", labelPosition: "stacked", value: (_g = this.step) === null || _g === void 0 ? void 0 : _g.component }), index.h("ion-item", { class: "json-editor-field", style: { zIndex: "3" } }, index.h("ion-label", null, index.h("h2", null, "Props"), index.h("fireenjin-code-editor", { autoExpand: true, outputObject: true, language: "json", name: "componentProps", value: (_h = this.step) === null || _h === void 0 ? void 0 : _h.componentProps }))))))), this.type === "fields" && (index.h("ion-card", null, index.h("ion-card-header", null, index.h("ion-card-title", null, "Fields", index.h("ion-button", { size: "small", style: { float: "right" }, disabled: !((_k = (_j = this.step) === null || _j === void 0 ? void 0 : _j.fields) === null || _k === void 0 ? void 0 : _k.length), onClick: () => this.toggleFieldsVisibility() }, this.showFields ? "Hide" : "Show", " Fields"))), this.showFields &&
        (((_l = this.step) === null || _l === void 0 ? void 0 : _l.fields) || []).map((field, index$1) => (index.h("ion-item", { button: true, onClick: () => this.fieldSelect(field) }, index.h("ion-icon", { color: "danger", slot: "end", onClick: (event) => this.deleteField(event, index$1), name: "trash" }), index.h("fireenjin-json-viewer", null, JSON.stringify(field, null, 4))))), index.h("fireenjin-form", { resetButton: null, submitButton: ((_o = (_m = this.step) === null || _m === void 0 ? void 0 : _m.fields) === null || _o === void 0 ? void 0 : _o.length) ? "update Field" : "Add Field", formData: this.step, name: "addFieldForm" }, index.h("ion-item", { class: "json-editor-field", style: { zIndex: "3" } }, index.h("ion-label", null, index.h("h2", null, "Add Fields"), index.h("fireenjin-code-editor", { ref: (el) => (this.fieldEl = el), autoExpand: true, outputObject: true, language: "json", name: "field", value: this.field || null })))))))), index.h("ion-tab", { tab: "steps" }, index.h("ion-content", null, index.h("ion-card", null, index.h("ion-item-divider", null, "Steps"), index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", null, (this.steps || []).map((step) => (index.h("ion-item", { title: `${step === null || step === void 0 ? void 0 : step.id} select a step to preview and edit`, button: true, onClick: () => this.stepSelect(step), style: { paddingBottom: ".5rem" } }, isAdmin.isAdmin() && store.state.claims.role === "tech" && (index.h("ion-button", { title: `Delete ${step === null || step === void 0 ? void 0 : step.name}`, color: "danger", onClick: () => this.deleteStep(event, step), slot: "end" }, "Delete")), index.h("ion-label", null, (step === null || step === void 0 ? void 0 : step.name) || "name")))))))))), index.h("ion-tab", { tab: "stepPreview" }, index.h("ion-content", null, index.h("ion-card", null, index.h("ion-item-divider", null, "Step Preview"), index.h("fireenjin-flow", { steps: this.stepPreview })))))),
    ];
  }
};
ModalAddStep.style = modalAddStepCss;

exports.floodteam_modal_add_step = ModalAddStep;
