import { ItemDataProvider } from '@dvn/core/components/views/dvn-app-view-detail-view-card/data-provider';
import { ItemFetchState } from '@dvn/core/models/item-fetch-state';
import { Component, h, Host, Listen, Method, Prop, State } from '@stencil/core';
export class DvnAppViewDetailViewCard {
  constructor() {
    this.showInfoDetailDialog = false;
    this.itemFetchState = ItemFetchState.init();
  }
  componentWillLoad() {
    const { apiData, params, view } = this;
    if (params === null || params === void 0 ? void 0 : params.item) {
      return;
    }
    const { query, table, ui } = view;
    const fields = (query === null || query === void 0 ? void 0 : query.fields) || ui.fields || view.table.fields;
    this.itemDataProvider = new ItemDataProvider(apiData, params, table, query, fields, (itemFetchState) => {
      this.itemFetchState = itemFetchState;
    });
    this.itemDataProvider.fetchData(this.itemFetchState);
  }
  // needed for invalidation
  componentDidUpdate() {
    const { params, itemFetchState } = this;
    if (params === null || params === void 0 ? void 0 : params.item) {
      return;
    }
    if (!itemFetchState.data && !itemFetchState.isPending && !itemFetchState.error) {
      this.itemDataProvider.fetchData(this.itemFetchState);
    }
  }
  async invalidate(_event) {
    this.itemDataProvider.fetchData(this.itemFetchState);
  }
  ;
  onAction(event) {
    const action = event.detail;
    if (action.name === 'open-dvn-card-view-detail-view') {
      event.preventDefault();
      this.showInfoDetailDialog = true;
    }
  }
  render() {
    var _a, _b, _c;
    const { itemFetchState, showInfoDetailDialog, view } = this;
    const { ui } = view;
    const fields = ui.fields || view.table.fields;
    const item = ((_a = this.params) === null || _a === void 0 ? void 0 : _a.item) || itemFetchState.data;
    return (h(Host, null,
      (!itemFetchState.isPending && !itemFetchState.error) &&
        (!ui.summaryCardConfig
          ? h("dvn-card-view", { key: `card-view-${item._id}`, config: ui.cardConfig, fields: fields, item: item, cornerRadius: ui.cardCornerRadius, leaned: true, overrideStyle: { width: '100%' } })
          : h("dvn-card-view", { key: `summary-card-view-${item._id}`, config: Object.assign(Object.assign({}, ui.summaryCardConfig), { footer: Object.assign(Object.assign({}, (ui.summaryCardConfig.footer || {})), { actions: [
                  ...(((_c = (_b = ui.summaryCardConfig) === null || _b === void 0 ? void 0 : _b.footer) === null || _c === void 0 ? void 0 : _c.actions) || []),
                  {
                    ui: {
                      label: 'See More',
                      name: 'open-dvn-card-view-detail-view',
                      view: 'flat'
                    }
                  }
                ] }) }), fields: fields, item: item, cornerRadius: ui.cardCornerRadius, leaned: true, overrideStyle: { width: '100%' } })),
      itemFetchState.isPending &&
        h("div", { class: "d-flex flex-grow-1 align-items-center justify-content-center detail-view-progress-container" },
          h("dvn-progress", { indefinite: true, tint: "primary" })),
      itemFetchState.error &&
        h("div", { class: "d-flex flex-grow-1 align-items-center justify-content-center flex-column detail-view-progress-container" },
          "Fetching data failed.",
          h("dvn-button", { onClick: () => this.itemDataProvider.fetchData(this.itemFetchState) }, "Retry")),
      showInfoDetailDialog &&
        h("dvn-dialog", { shown: this.showInfoDetailDialog, onDismiss: () => this.showInfoDetailDialog = false, raw: true },
          h("dvn-card-view", { key: `detail-card-view-${item._id}`, config: ui.cardConfig, fields: fields, item: item, cornerRadius: ui.cardCornerRadius, leaned: true, overrideStyle: { width: '100%' } }))));
  }
  static get is() { return "dvn-app-view-detail-view-card"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-app-view-detail-view-card.css"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-app-view-detail-view-card.css"]
  }; }
  static get properties() { return {
    "apiData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DataApi",
        "resolved": "DataApi",
        "references": {
          "DataApi": {
            "location": "import",
            "path": "@dvn/core/api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "view": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DetailViewConfig",
        "resolved": "DetailViewConfig",
        "references": {
          "DetailViewConfig": {
            "location": "import",
            "path": "@dvn/core/models/detail-view-config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "params": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DetailViewParams",
        "resolved": "DetailViewParams",
        "references": {
          "DetailViewParams": {
            "location": "import",
            "path": "@dvn/core/components/views/dvn-app-view-detail-view-card/params"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "showInfoDetailDialog": {},
    "itemFetchState": {}
  }; }
  static get methods() { return {
    "invalidate": {
      "complexType": {
        "signature": "(_event: CustomEvent<InvalidationAction>) => Promise<void>",
        "parameters": [{
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "CustomEvent": {
            "location": "global"
          },
          "InvalidationAction": {
            "location": "import",
            "path": "@dvn/core/models/actions"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
