import { ItemDataProvider } from '@dvn/core/components/views/dvn-app-view-detail-view/data-provider';
import { ItemFetchState } from '@dvn/core/models/item-fetch-state';
import { Component, h, Host, Method, Prop, State } from '@stencil/core';
export class DvnAppViewDetailView {
  constructor() {
    this.itemFetchState = ItemFetchState.init();
  }
  componentWillLoad() {
    const { apiData, params, view } = this;
    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 { itemFetchState } = this;
    if (!itemFetchState.data && !itemFetchState.isPending && !itemFetchState.error) {
      this.itemDataProvider.fetchData(this.itemFetchState);
    }
  }
  async invalidate(_event) {
    this.itemDataProvider.fetchData(this.itemFetchState);
  }
  ;
  render() {
    const { itemFetchState, view } = this;
    const { ui } = view;
    const fields = ui.fields || view.table.fields;
    const item = itemFetchState.data;
    return (h(Host, null,
      (!itemFetchState.isPending && !itemFetchState.error) &&
        h("dvn-card-view", { config: ui.cardConfig, fields: fields, item: item, cornerRadius: 0, 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"))));
  }
  static get is() { return "dvn-app-view-detail-view"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-app-view-detail-view.css"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-app-view-detail-view.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/params"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "viewId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "view-id",
      "reflect": false
    }
  }; }
  static get states() { return {
    "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": []
      }
    }
  }; }
}
