import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import "smoothly";
import { Item } from "@payfunc/model";
import * as translation from "./translation";
export class PayfuncItemInput {
  constructor() {
    this.item = {};
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  clearInputs(e) {
    if (e.detail.name == "clear") {
      e.stopPropagation();
      e.preventDefault();
      const quantity = document.getElementById("itemQuantity");
      const unit = document.getElementById("itemUnit");
      const name = document.getElementById("itemName");
      const price = document.getElementById("itemPrice");
      const vat = document.getElementById("itemVat");
      const inputs = [quantity, unit, name, price, vat];
      for (const input of inputs)
        if (hasStringValue(input) || hasNumberValue(input))
          input.value = "";
      this.updateItem();
      this.itemCleared.emit();
    }
  }
  updateItem() {
    const quantity = document.getElementById("itemQuantity");
    const unit = document.getElementById("itemUnit");
    const name = document.getElementById("itemName");
    const price = document.getElementById("itemPrice");
    const vat = document.getElementById("itemVat");
    const item = Item.fromVatInclusivePrice(hasNumberValue(price) ? price.value : 0, hasNumberValue(vat) ? vat.value : undefined, undefined, hasStringValue(name) ? name.value : undefined, hasStringValue(quantity) && Number(quantity.value) > 0 ? Number(quantity.value) : undefined, hasStringValue(unit) ? unit.value : undefined, undefined);
    this.itemChanged.emit(item);
  }
  render() {
    const t = this.t;
    return [
      h("form", { name: "item" },
        h("smoothly-input", { class: "left small", onKeyDown: this.updateItem.bind(this), id: "itemQuantity", value: this.item.quantity, type: "text", name: "quantity" }, t("Qty")),
        h("smoothly-input", { class: "small", onKeyDown: this.updateItem.bind(this), id: "itemUnit", value: this.item.unit, type: "text", name: "unit" }, t("Unit")),
        h("smoothly-input", { class: "big", onKeyDown: this.updateItem.bind(this), id: "itemName", value: this.item.name, type: "text", name: "name" }, t("Name")),
        h("smoothly-input", { class: "medium", onKeyDown: this.updateItem.bind(this), id: "itemPrice", value: this.inclusivePrice, type: "price", name: "price" }, t("Price")),
        h("smoothly-input", { class: "medium", onKeyDown: this.updateItem.bind(this), id: "itemVat", value: this.vatPercentage, type: "percent", name: "vat" }, t("VAT")),
        h("smoothly-trigger", { fill: "clear", name: "clear" },
          h("smoothly-icon", { toolTip: t("Clear/remove item"), size: "small", name: "trash" }))),
    ];
  }
  static get is() { return "payfunc-item-input"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "item": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "Item",
        "resolved": "Item",
        "references": {
          "Item": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    }
  }; }
  static get states() { return {
    "t": {},
    "inclusivePrice": {},
    "vatPercentage": {}
  }; }
  static get events() { return [{
      "method": "itemChanged",
      "name": "itemChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Item",
        "resolved": "Item",
        "references": {
          "Item": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      }
    }, {
      "method": "itemCleared",
      "name": "itemCleared",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "trigger",
      "method": "clearInputs",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
function hasStringValue(element) {
  return typeof element.value === "string";
}
function hasNumberValue(element) {
  return typeof element.value === "number";
}
