import { Component, Element, Event, h, Host, Listen, Prop, State } from '@stencil/core';
export class DvnTableCell {
  constructor() {
    this.focused = false;
    this.isValueChanged = false;
  }
  onAction(event) {
    if (!this.editable)
      return;
    event.stopPropagation();
    this.modifiedValue = event.detail;
    this.isValueChanged = true;
  }
  completeBlur() {
    if (this.isValueChanged) {
      this.cellValueChange.emit(this.modifiedValue);
      this.modifiedValue = undefined;
      this.isValueChanged = false;
    }
    this.focused = false;
  }
  render() {
    var _a, _b;
    const { focused, weight, width } = this;
    const style = {};
    if (weight) {
      style['--cell-weight'] = weight;
      style['--cell-width'] = `0px`;
    }
    else {
      style['--cell-weight'] = `0 0 ${width}px`;
      style['--cell-width'] = `${width}px`;
    }
    return (h(Host, { class: { focused }, style: style, tabindex: "0", onFocus: () => {
        if (!this.editable)
          return;
        this.focused = true;
      }, onBlur: (e) => {
        if (!this.editable)
          return;
        const targetNode = e.relatedTarget;
        if (!targetNode || targetNode.closest('dvn-table-cell') !== this.el) {
          // some node outside of this component is focused
          this.completeBlur();
        }
        else {
          // stay focused for keeping rendering the edit input
          this.el.focus();
        }
      } },
      focused ? (_a = this.renderEdit) === null || _a === void 0 ? void 0 : _a.call(this) : (_b = this.renderDisplay) === null || _b === void 0 ? void 0 : _b.call(this),
      h("slot", null)));
  }
  static get is() { return "dvn-table-cell"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-table-cell.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-table-cell.css"]
  }; }
  static get properties() { return {
    "renderDisplay": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "render-display",
      "reflect": false
    },
    "renderEdit": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "render-edit",
      "reflect": false
    },
    "weight": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "weight",
      "reflect": false
    },
    "width": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "width",
      "reflect": false
    },
    "editable": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "editable",
      "reflect": false
    }
  }; }
  static get states() { return {
    "focused": {},
    "modifiedValue": {},
    "isValueChanged": {}
  }; }
  static get events() { return [{
      "method": "cellValueChange",
      "name": "cellValueChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "el"; }
  static get listeners() { return [{
      "name": "change",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
