'use strict';

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

const index = require('./index-e0385008.js');
const tslib_es6 = require('./tslib.es6-8eb3c9db.js');
const index_min = require('./index.min-0ca13044.js');
require('./_commonjsHelpers-a5111d61.js');

const Logic = class {
  constructor(hostRef) {
    index.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 = {
      ...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 [
      index.h("fireenjin-code-editor", { autoExpand: true, value: JSON.stringify(this.value, null, 4), language: "json", name: this.name }),
      index.h("ion-item-divider", null, index.h("ion-label", null, "Statements"), index.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,
          };
        } }, index.h("ion-select-option", null, "and"), index.h("ion-select-option", null, "or"))),
      index.h("ion-item", null, index.h("div", null, (this.statements || []).map((statement, index$1) => (index.h("ion-chip", null, index.h("ion-label", { innerHTML: JSON.stringify(statement) }), index.h("ion-icon", { name: "close-circle", onClick: () => this.removeStatement(index$1) })))), !((_a = this.statements) === null || _a === void 0 ? void 0 : _a.length) && (index.h("ion-label", null, "Add Statements to Fire Action")))),
      index.h("ion-item", null, index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", { style: {
          maxWidth: "50px",
        } }, index.h("ion-button", { fill: "clear", id: "select-type" }, index.h("ion-icon", { slot: "icon-only", name: this.selectedType === "number"
          ? "calculator"
          : this.selectedType === "string"
            ? "pencil"
            : this.selectedType === "variable"
              ? "code"
              : "bulb" })), index.h("ion-popover", { "keep-contents-mounted": "true", trigger: "select-type", dismissOnSelect: true }, index.h("ion-content", null, index.h("ion-list", null, index.h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = null) }, "Auto"), index.h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "string") }, "String"), index.h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "number") }, "Number"), index.h("ion-item", { href: "#", detail: true, onClick: () => (this.selectedType = "variable") }, "Variable"))))), index.h("ion-col", null, index.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();
        } }), index.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]) => (index.h("ion-select-option", { value: `{"var":"${key}"}` }, (control === null || control === void 0 ? void 0 : control.label) || key))))), index.h("ion-col", { style: { minWidth: "160px" } }, index.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) => (index.h("ion-select-option", { value: value }, value))))), index.h("ion-col", null, index.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" })), index.h("ion-col", null, index.h("ion-button", { fill: "clear", onClick: () => this.toggleManualEdit() }, index.h("ion-icon", { slot: "icon-only", name: "create" })), index.h("ion-button", { color: "primary", onClick: async () => {
          await this.updateSelectedOperation();
          this.addStatment(this.selectedOperation);
        } }, index.h("ion-icon", { slot: "icon-only", name: "add-circle" })))), index.h("ion-row", { style: {
          opacity: this.manualEdit ? "1" : "0",
          height: this.manualEdit ? "auto" : "0",
        } }, index.h("ion-col", null, index.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" }))))),
    ];
  }
};
tslib_es6.__decorate([
  index_min.index_min.debounce(2000)
], Logic.prototype, "leftAndRightFromSelectedOperation", null);

exports.floodteam_logic = Logic;
