var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { generateActionComponent } from '@dvn/core/components/views/dvn-card-view/action-util';
import { getDataModelFieldDisplayValue } from '@dvn/core/models/data-model';
import { findField } from '@dvn/core/util/field';
import { Component, Event, h, Host, Prop } from '@stencil/core';
function getFieldFromData(fields, fieldData) {
  return fieldData.fieldDisplay || findField(fields, fieldData.fieldId);
}
function getColumnData(config, field, fieldId) {
  if (!config.columns || (!config.columns[field.id] && !config.columns[fieldId])) {
    return {
      weight: 1,
    };
  }
  return config.columns[field.id] || config.columns[fieldId];
}
function calculateActionsWidth(actions = []) {
  return actions.length * 40;
}
const renderRow = (config, fields, tableColumnConfig, actionEvent, modifyItemEvent, item, params, index, totalWidth, actionsWidth) => (h("dvn-table-row", { elevation: config.elevation, separation: config.separation, removeSpacing: true, width: `${totalWidth}px` },
  tableColumnConfig.map(columnConfig => {
    const { fieldId, fieldDisplay } = columnConfig, meta = __rest(columnConfig, ["fieldId", "fieldDisplay"]);
    const field = getFieldFromData(fields, columnConfig);
    const columnData = getColumnData(config, field, fieldId);
    if (columnConfig.componentTag) {
      const Tag = columnConfig.componentTag;
      return (h("dvn-table-cell", { weight: columnData.weight, width: columnData.width },
        h(Tag, { field: field, item: item, meta: meta })));
    }
    return (h("dvn-table-cell", { editable: columnConfig.isEditable, weight: columnData.weight, width: columnData.width, onCellValueChange: (event) => {
        event.stopPropagation();
        modifyItemEvent.emit({
          field,
          item,
          value: event.detail,
        });
      }, renderDisplay: () => (h("dvn-field-display-cell", { showTitle: false, field: field, value: getDataModelFieldDisplayValue(item, field, columnConfig.fieldId), meta: meta })), renderEdit: () => (h("dvn-field-input-cell", { field: field, value: getDataModelFieldDisplayValue(item, field, columnConfig.fieldId) })) }));
  }),
  (config.actions && config.actions.length > 0) &&
    h("dvn-table-cell", { width: actionsWidth }, config.actions.map(action => generateActionComponent(index, params, item, actionEvent, action, { leaned: true })))));
const renderHeader = (config, fields, tableColumnConfig, setRef, totalWidth, actionsWidth) => {
  return (h("dvn-table-header", { elevation: config.elevation, separation: config.separation, width: `${totalWidth}px`, ref: setRef },
    tableColumnConfig.map(columnConfig => {
      const field = getFieldFromData(fields, columnConfig);
      return (h("dvn-table-header-cell", { weight: getColumnData(config, field, columnConfig.fieldId).weight, width: getColumnData(config, field, columnConfig.fieldId).width }, field.title));
    }),
    (config.actions && config.actions.length > 0) &&
      h("dvn-table-header-cell", { width: actionsWidth })));
};
export class DvnListViewTable {
  constructor() {
    this.items = [];
    this.isEndReached = false;
    this.isProgressing = false;
    this.fields = [];
    this.params = {};
    this.scrollToFit = false;
  }
  render() {
    const { items, config, params } = this;
    const { elevation, fields: tableColumnConfigs, hideHeader = false, separation = 0 } = config;
    const actionsWidth = config.actionsWidth || calculateActionsWidth(config.actions) || 0;
    let totalWidth = actionsWidth || 0;
    tableColumnConfigs.forEach(columnConfig => {
      const field = getFieldFromData(this.fields, columnConfig);
      totalWidth += getColumnData(config, field, columnConfig.fieldId).width || 100;
    });
    return (h(Host, null,
      h("dvn-table", { leaned: true, scrollToFit: this.scrollToFit },
        !hideHeader && renderHeader(config, this.fields, tableColumnConfigs, el => this.tableHeaderRef = el, totalWidth, actionsWidth),
        h("dvn-table-body", { elevation: elevation, separation: separation, ref: el => this.tableBodyRef = el, onScroll: () => this.tableHeaderRef.scrollLeft = this.tableBodyRef.scrollLeft, scrollToFit: this.scrollToFit },
          items.map((item, index) => renderRow(config, this.fields, tableColumnConfigs, this.action, this.modifyItem, item, params, index, totalWidth, actionsWidth)),
          !this.isProgressing && !this.isEndReached && !this.error && items.length === 0 &&
            h("p", { class: "text-centered" }, "No data found."),
          !this.isEndReached &&
            h("div", { class: "table-progress-container" },
              this.isProgressing &&
                h("dvn-progress", { indefinite: true, tint: "primary" }),
              this.error &&
                h("div", { class: "error-container d-flex align-items-center justify-content-center flex-column" },
                  this.error,
                  h("dvn-button", { onClick: () => this.retry.emit() }, "Retry")))))));
  }
  static get is() { return "dvn-list-view-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-list-view-table.css"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-list-view-table.css"]
  }; }
  static get properties() { return {
    "config": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "ListViewTableConfig",
        "resolved": "ListViewTableConfig",
        "references": {
          "ListViewTableConfig": {
            "location": "import",
            "path": "@dvn/core/components/views/dvn-list-view-table/config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "items": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DataModel[]",
        "resolved": "DataModel[]",
        "references": {
          "DataModel": {
            "location": "import",
            "path": "@dvn/core/models/data-model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    },
    "isEndReached": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-end-reached",
      "reflect": false,
      "defaultValue": "false"
    },
    "isProgressing": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-progressing",
      "reflect": false,
      "defaultValue": "false"
    },
    "error": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "error",
      "reflect": false
    },
    "fields": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "BaseField[]",
        "resolved": "BaseField[]",
        "references": {
          "BaseField": {
            "location": "import",
            "path": "@dvn/core/doldurma/src/BaseField"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    },
    "params": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "params",
      "reflect": false,
      "defaultValue": "{}"
    },
    "scrollToFit": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "scroll-to-fit",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get events() { return [{
      "method": "action",
      "name": "action",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "retry",
      "name": "retry",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "modifyItem",
      "name": "modifyItem",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
}
