import { r as registerInstance, h } from './index-e0adbd54.js';
import { i as index_min } from './index.min-3591c16e.js';
import './_commonjsHelpers-44457d8d.js';

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const InputLogic = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.leftSide = null;
    this.rightSide = null;
    this.comparableOperations = [
      "==",
      "===",
      "!=",
      "!==",
      "!",
      "!==",
      "!",
      "!!",
      ">",
      ">=",
      ">=",
      "<",
      "<=",
      "+",
      "-",
      "*",
      "/",
      "%",
    ];
    this.name = "logic";
    this.operations = {
      var: { var: "" },
      missing: { missing: ["", ""] },
      missing_some: { missing_some: ["", [""]] },
      if: { if: ["", true, false] },
      "==": { "==": ["", ""] },
      "===": { "===": ["", ""] },
      "!=": { "!=": ["", ""] },
      "!==": { "!==": ["", ""] },
      "!": { "!": ["", ""] },
      "!!": { "!!": ["", ""] },
      or: { or: [] },
      and: { and: [] },
      ">": { ">": [1, 0] },
      ">=": { ">=": [1, 0] },
      "<": { "<": [0, 1] },
      "<=": { "<=": [0, 1] },
      min: { min: [0, 1] },
      max: { max: [0, 1] },
      "+": { "+": [0, 1] },
      "-": { "-": [0, 1] },
      "*": { "*": [0, 1] },
      "/": { "/": [0, 1] },
      "%": { "%": [0, 1] },
      map: { map: [{ var: "" }, {}] },
      filter: { filter: [{ var: "" }, {}] },
      reduce: {
        reduce: [
          { var: "" },
          { "+": [{ var: "current" }, { var: "accumulator" }] },
          0,
        ],
      },
      all: { all: [[1, 2, 3], { ">": [{ var: "" }, 0] }] },
      some: { some: [[-1, 0, 1], { ">": [{ var: "" }, 0] }] },
      none: { none: [[-3, -2, -1], { ">": [{ var: "" }, 0] }] },
      merge: {
        merge: [
          [1, 2],
          [3, 4],
        ],
      },
      in: { in: ["", [""]] },
      cat: { cat: ["", ""] },
      substr: { substr: ["", 0] },
      log: { log: "" },
    };
    this.joinBy = "and";
    this.value = this.joinBy === "and" ? this.operations.and : this.operations.or;
    this.selectedOperator = "==";
    this.selectedType = undefined;
    this.variables = {};
    this.selectedOperation = this.operations[this.selectedOperator];
    this.statements = [];
    this.manualEdit = true;
  }
  async addStatment(statement) {
    this.statements = [...this.statements, statement];
    this.value = {
      [this.joinBy]: this.statements,
    };
  }
  async removeStatement(index) {
    this.statements = this.statements.filter((_statement, i) => i !== index);
    this.value = {
      [this.joinBy]: this.statements,
    };
  }
  async toggleManualEdit() {
    this.manualEdit = !this.manualEdit;
  }
  async updateSelectedOperation() {
    const [operationKey] = Object.keys(this.selectedOperation);
    this.selectedOperation = Object.assign(Object.assign({}, this.selectedOperation), { [operationKey]: [this.leftSide, this.rightSide] });
  }
  async leftAndRightFromSelectedOperation() {
    const [operationKey] = Object.keys(this.selectedOperation);
    const leftHandValue = this.selectedOperation[operationKey][0];
    this.leftSide =
      typeof leftHandValue === "string" &&
        (!leftHandValue.includes("{") || !leftHandValue.includes("["))
        ? leftHandValue
        : JSON.stringify(leftHandValue);
    this.leftInputEl.value = this.leftSide;
    const rightHandValue = this.selectedOperation[operationKey][1];
    this.rightSide =
      typeof rightHandValue === "string" &&
        (!rightHandValue.includes("{") || !rightHandValue.includes("["))
        ? rightHandValue
        : JSON.stringify(rightHandValue);
    this.rightInputEl.value = this.rightSide;
  }
  componentDidLoad() {
    setTimeout(() => {
      this.manualEdit = false;
    }, 1000);
  }
  render() {
    var _a;
    const selectedOperationJSON = JSON.stringify(this.selectedOperation, null, 4);
    return [
      h("fireenjin-code-editor", { autoExpand: true, value: JSON.stringify(this.value, null, 4), language: "json", name: this.name }),
      h("ion-item-divider", null, h("ion-label", null, "Statements"), h("ion-select", { slot: "end", name: "join", interface: "popover", value: this.joinBy, onIonChange: (event) => {
          this.joinBy = event.target.value;
          this.value = {
            [this.joinBy]: this.statements,
          };
        } }, h("ion-select-option", null, "and"), h("ion-select-option", null, "or"))),
      h("ion-item", null, h("div", null, (this.statements || []).map((statement, index) => (h("ion-chip", null, h("ion-label", { innerHTML: JSON.stringify(statement) }), h("ion-icon", { name: "close-circle", onClick: () => this.removeStatement(index) })))), !((_a = this.statements) === null || _a === void 0 ? void 0 : _a.length) && (h("ion-label", null, "Add Statements to Fire Action")))),
      h("ion-item", null, h("ion-grid", null, h("ion-row", null, h("ion-col", { style: {
          maxWidth: "50px",
        } }, h("ion-button", { fill: "clear", id: "select-type" }, h("ion-icon", { slot: "icon-only", name: this.selectedType === "number"
          ? "calculator"
          : this.selectedType === "string"
            ? "pencil"
            : this.selectedType === "variable"
              ? "code"
              : "bulb" })), h("ion-popover", { "keep-contents-mounted": "true", trigger: "select-type", dismissOnSelect: true }, h("ion-content", null, h("ion-list", null, h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = null) }, "Auto"), h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "string") }, "String"), h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "number") }, "Number"), h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "variable") }, "Variable"))))), h("ion-col", null, h("ion-input", { ref: (el) => (this.leftInputEl = el), style: {
          minWidth: "200px",
          display: this.selectedType === "variable" ? "none" : "block",
        }, placeholder: "Left Value", onIonChange: async (event) => {
          var _a, _b;
          try {
            this.leftSide =
              typeof ((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value) !== "undefined"
                ? JSON.parse(event.target.value)
                : null;
          }
          catch (e) {
            console.log("Error parsing as JSON: ", e);
            this.leftSide = ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value) || null;
          }
          await this.updateSelectedOperation();
        } }), h("ion-select", { onIonChange: (event) => {
          var _a, _b;
          try {
            this.leftSide =
              typeof ((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value) !== "undefined"
                ? JSON.parse(event.target.value)
                : null;
          }
          catch (e) {
            console.log("Error parsing as JSON: ", e);
            this.leftSide = ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value) || null;
          }
        }, style: {
          minWidth: "200px",
          display: this.selectedType === "variable" ? "flex" : "none",
        }, placeholder: "Select Variable" }, Object.entries(this.variables || {}).map(([key, control]) => (h("ion-select-option", { value: `{"var":"${key}"}` }, (control === null || control === void 0 ? void 0 : control.label) || key))))), h("ion-col", { style: { minWidth: "160px" } }, h("ion-select", { name: "operator", interface: "popover", value: this.selectedOperator, onIonChange: (event) => {
          this.selectedOperator = event.target.value;
          this.selectedOperation =
            this.operations[this.selectedOperator];
        } }, Object.keys(this.operations || {})
        .filter((value) => this.comparableOperations.includes(value))
        .map((value) => (h("ion-select-option", { value: value }, value))))), h("ion-col", null, h("ion-input", { type: this.selectedType === "number" ? "number" : "text", ref: (el) => (this.rightInputEl = el), onIonChange: async (event) => {
          var _a, _b;
          try {
            this.rightSide =
              typeof ((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value) !== "undefined"
                ? JSON.parse(event.target.value)
                : null;
          }
          catch (e) {
            console.log("Error parsing as JSON: ", e);
            this.rightSide = ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value) || null;
          }
          await this.updateSelectedOperation();
        }, placeholder: "Right Value" })), h("ion-col", null, h("ion-button", { fill: "clear", onClick: () => this.toggleManualEdit() }, h("ion-icon", { slot: "icon-only", name: "create" })), h("ion-button", { color: "primary", onClick: async () => {
          await this.updateSelectedOperation();
          this.addStatment(this.selectedOperation);
        } }, h("ion-icon", { slot: "icon-only", name: "add-circle" })))), h("ion-row", { style: {
          opacity: this.manualEdit ? "1" : "0",
          height: this.manualEdit ? "auto" : "0",
        } }, h("ion-col", null, h("fireenjin-code-editor", { onFireenjinCodeChange: (event) => {
          var _a;
          if (!this.manualEdit ||
            ((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.value) === selectedOperationJSON)
            return;
          try {
            this.selectedOperation = JSON.parse(event.detail.value);
            this.leftAndRightFromSelectedOperation();
          }
          catch (e) {
            console.log("Error parsing selected operation as JSON: ", e);
          }
        }, autoExpand: true, value: selectedOperationJSON, language: "json" }))))),
    ];
  }
};
__decorate([
  index_min.debounce(2000)
], InputLogic.prototype, "leftAndRightFromSelectedOperation", null);

export { InputLogic as fireenjin_input_logic };
