import { User } from '@dvn/kimlik/api/models';
import { Component, h, Host, Listen, Prop, State } from '@stencil/core';
import '@stencil/router';
import { Product } from '../../../models/product';
import { WorkspaceOwnership } from '../../../models/workspace-ownership';
export class DevingenRoot {
  constructor() {
    this.fetchSession = async () => {
      this.fetchSessionError = undefined;
      const sessionResponse = await this.apiKimlik.getSession().catch((e) => {
        this.fetchSessionError = e;
      });
      if (!sessionResponse)
        return;
      this.user = User.decode(sessionResponse.user);
    };
    this.fetchWorkspaces = async () => {
      const workspaceOwnershipsResponse = await this.apiDevingen.getWorkspaceOwnerships().catch((e) => {
        this.fetchWorkspacesError = e;
      });
      if (!workspaceOwnershipsResponse)
        return;
      this.workspaceOwnerships = workspaceOwnershipsResponse.results.map(i => WorkspaceOwnership.decode(i));
      const productsResponse = await this.apiDevingen.getProducts().catch((e) => {
        this.fetchWorkspacesError = e;
      });
      if (!productsResponse)
        return;
      this.products = productsResponse.results.map(i => Product.decode(i));
    };
  }
  onInvalidation(_event) {
    // new workspace or product is created
    this.fetchWorkspaces();
  }
  ;
  componentWillLoad() {
    this.fetchSession();
    this.fetchWorkspaces();
  }
  render() {
    const { apiData, apiDevingen, apiKimlik, fetchSessionError, fetchWorkspacesError, products, workspaceOwnerships, user, } = this;
    if (fetchSessionError) {
      return (h(Host, null,
        h("devingen-top-bar", { user: user }),
        h("div", { class: "d-flex justify-content-center" },
          h("devingen-error-card", { error: fetchSessionError, message: "Failed to get session info.", onRetry: this.fetchSession }))));
    }
    if (fetchWorkspacesError) {
      return (h(Host, null,
        h("div", { class: "d-flex justify-content-center" },
          h("devingen-error-card", { error: fetchWorkspacesError, message: "Failed to get workspace info.", onRetry: this.fetchWorkspaces }))));
    }
    return (h(Host, null,
      h("devingen-top-bar", { user: user }),
      h("stencil-router", null,
        h("stencil-route-switch", { scrollTopOffset: 0 },
          h("stencil-route", { url: "/", component: "devingen-home-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships }, exact: true }),
          h("stencil-route", { url: "/products/:id", component: "devingen-product-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships } }),
          h("stencil-route", { component: "devingen-page-not-found" })))));
  }
  static get is() { return "devingen-router"; }
  static get properties() { return {
    "apiKimlik": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "KimlikApi",
        "resolved": "KimlikApi",
        "references": {
          "KimlikApi": {
            "location": "import",
            "path": "@dvn/kimlik/api/kimlikApi"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "apiDevingen": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DevingenApi",
        "resolved": "DevingenApi",
        "references": {
          "DevingenApi": {
            "location": "import",
            "path": "../../../api/devingen-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "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": ""
      }
    }
  }; }
  static get states() { return {
    "user": {},
    "fetchSessionError": {},
    "products": {},
    "workspaceOwnerships": {},
    "fetchWorkspacesError": {}
  }; }
  static get listeners() { return [{
      "name": "invalidation",
      "method": "onInvalidation",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
