import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { Notice } from "smoothly";
import { Merchant } from "@payfunc/model-acquirer";
import { hasValue, ModelState } from "../../../../../model";
import * as translation from "../translation";
export class IntergiroMerchantRuleInput {
  constructor() {
    this.add = false;
    this.action = "reject";
    this.event = "authorization";
    this.textEntered = false;
    this.actions = ["reject"];
    this.events = ["authorization", "refund", "capture", "void"];
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
    this.setType();
  }
  setType() {
    var _a;
    if (!this.type)
      this.type = ((_a = this.values) === null || _a === void 0 ? void 0 : _a.includes("master")) ? "acquirer" : "agent";
  }
  onSubmit(e) {
    var _a;
    const key = (_a = e.detail.key) !== null && _a !== void 0 ? _a : (this.type == "acquirer" ? "master" : this.type);
    const rule = this.action + " " + this.event + " if " + this.ruleElement.value;
    if (key && Merchant.Rule.parse(rule)) {
      this.updated.emit({
        key,
        value: rule,
      });
      this.reset();
    }
    else
      this.notice.emit(Notice.failed(this.t("Invalid input")));
  }
  onCancel(e) {
    this.cancel.emit(e);
    this.reset();
  }
  onSelectionChanged(e) {
    if (e.detail.identifier == "type")
      this.type = e.detail.value;
    else if (e.detail.identifier == "action")
      this.action = e.detail.value;
    else if (e.detail.identifier == "event")
      this.event = e.detail.value;
    if (!this.add)
      this.textEntered = true;
  }
  checkTextEntered() {
    if (hasValue(this.ruleElement))
      this.textEntered = this.ruleElement.value != "";
  }
  reset() {
    if (this.ruleElement && hasValue(this.ruleElement))
      this.ruleElement.value = "";
    this.textEntered = false;
    this.setType();
  }
  render() {
    var _a, _b, _c;
    return (h("form", null,
      this.add && [
        h("smoothly-select", { identifier: "type", background: "transparent" }, (_a = this.values) === null || _a === void 0 ? void 0 : _a.map(v => (h("option", { value: v, selected: this.type == v }, this.t(v == "master" ? "Acquirer" : v.charAt(0).toUpperCase() + v.slice(1)))))),
        this.type == "other" && (h("smoothly-input", { id: "type", name: "key" }, this.t("Rule by"))),
      ],
      h("section", null,
        h("smoothly-select", { identifier: "action", background: "transparent" }, (_b = this.actions) === null || _b === void 0 ? void 0 : _b.map(a => (h("option", { value: a, selected: this.action == a }, a)))),
        h("span", null, "-"),
        h("smoothly-select", { identifier: "event", background: "transparent" }, (_c = this.events) === null || _c === void 0 ? void 0 : _c.map(e => (h("option", { value: e, selected: this.event == e, id: this.event == e ? "selected" : "" }, e)))),
        h("span", null, "if"),
        h("ig-autocomplete-input", { id: "rule", name: "value", template: ModelState.Authorization.template, value: this.value, ref: (element) => (this.ruleElement = element), onKeyDown: this.checkTextEntered.bind(this) }),
        h("smoothly-submit", { fill: "clear", prevent: true, onSubmit: (e) => this.onSubmit(e), style: { visibility: this.textEntered ? "visible" : "hidden" } },
          h("smoothly-icon", { toolTip: this.t("Add rule"), name: "checkmark-circle", size: "small" })),
        h("smoothly-submit", { fill: "clear", prevent: true, onClick: (e) => this.onCancel(e) },
          h("smoothly-icon", { toolTip: this.t("Cancel"), size: "small", name: "close-circle" })))));
  }
  static get is() { return "ig-merchant-rule-input"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "value": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "value",
      "reflect": false
    },
    "add": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean | undefined",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "add",
      "reflect": false,
      "defaultValue": "false"
    },
    "values": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "string[]",
        "resolved": "string[] | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "type": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "type",
      "reflect": false
    },
    "action": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "action",
      "reflect": false,
      "defaultValue": "\"reject\""
    },
    "event": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "event",
      "reflect": false,
      "defaultValue": "\"authorization\""
    }
  }; }
  static get states() { return {
    "t": {},
    "textEntered": {}
  }; }
  static get events() { return [{
      "method": "updated",
      "name": "updated",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{ key?: string; value: string }",
        "resolved": "{ key?: string | undefined; value: string; }",
        "references": {}
      }
    }, {
      "method": "cancel",
      "name": "cancel",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "notice",
      "name": "notice",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Notice",
        "resolved": "Notice",
        "references": {
          "Notice": {
            "location": "import",
            "path": "smoothly"
          }
        }
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "selectionChanged",
      "method": "onSelectionChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
