import { ItemDataProvider } from '@dvn/core/components/views/dvn-app-view-detail-view-card/data-provider';
import { convertValues } from '@dvn/core/components/views/dvn-card-view/action-util';
import { ItemFetchState } from '@dvn/core/models/item-fetch-state';
import { Component, Element, h, Host, Listen, Method, Prop, State } from '@stencil/core';
function getViewComponentName(view) {
  return `dvn-app-view-${view.type}`;
}
export class DvnAppViewCompositeViewLinear {
  constructor() {
    this.itemFetchState = ItemFetchState.init();
    this.needsToFetchData = false;
  }
  componentWillLoad() {
    const { apiData, params = {}, view } = this;
    this.needsToFetchData = !!params.itemId;
    if (!this.needsToFetchData) {
      return;
    }
    const { query, table } = view;
    const fields = (query === null || query === void 0 ? void 0 : query.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);
    }
  }
  onInvalidation(event) {
    var _a;
    if (this.view._id === event.detail.targetViewId) {
      this.invalidate(event);
    }
    else {
      // TODO this may not be needed if there is dvn-app wrapper around this view
      //  and that handles invalidation. there is no dvn-app wrapper for mentornity views
      // invalidate child
      (_a = this.el.querySelector(`#${event.detail.targetViewId}`)) === null || _a === void 0 ? void 0 : _a.invalidate(event);
    }
  }
  ;
  async invalidate(_event) {
    this.itemDataProvider.fetchData(this.itemFetchState);
  }
  ;
  render() {
    const { appConfig, itemFetchState, needsToFetchData, view } = this;
    const { ui } = view;
    const item = itemFetchState.data;
    const gutter = ui.gutters || 'calc(4 * var(--spacing-unit)';
    const shouldBlankTitlesAllocateSpace = ui.layout === 'horizontal' && ui.views.some(vc => !!vc.title);
    const context = {
      appConfig,
      apiAtama: this.apiAtama,
      apiData: this.apiData,
      apiTakvim: this.apiTakvim,
      apiUye: this.apiUye,
      appContextInvalidator: this.appContextInvalidator,
      logger: this.logger,
      membership: this.membership,
      navigate: this.navigate,
      notifier: this.notifier,
      user: this.user,
      userSettings: this.userSettings,
    };
    return (h(Host, { class: {
        [ui.layout]: true,
      } },
      (!needsToFetchData || (itemFetchState.data || (!itemFetchState.isPending && !itemFetchState.error))) &&
        h("div", { class: "view-wrappers-container", style: {
            flexDirection: ui.layout === 'vertical' ? 'column' : 'row',
          } }, ui.views.map(compositeViewConfig => {
          const { description, height, params = { conversionConfig: {} }, title, viewId, viewTag, weights } = compositeViewConfig;
          let ViewTag = viewTag;
          let viewConfig;
          if (!ViewTag) {
            viewConfig = appConfig.getView(viewId);
            if (viewConfig) {
              ViewTag = getViewComponentName(viewConfig);
            }
          }
          const viewParams = params
            ? Object.assign(Object.assign({}, params), convertValues(params.conversionConfig, { item })) : {};
          return (h("div", { class: "view-wrapper", style: {
              '--composite-view-gutter': gutter,
              '--weigh': `${(weights === null || weights === void 0 ? void 0 : weights.small) || 1}`,
              height,
            } },
            (title || shouldBlankTitlesAllocateSpace) &&
              h("div", { class: "ml-2 mb-2" },
                h("small", null,
                  h("b", null,
                    title,
                    "\u00A0")),
                description &&
                  h("small", null,
                    h("br", null),
                    description)),
            h(ViewTag, Object.assign({}, context, { key: viewId, id: viewId, item: item, params: viewParams, view: viewConfig }))));
        })),
      needsToFetchData && itemFetchState.isPending && !itemFetchState.data &&
        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-composite-view-linear"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-app-view-composite-view-linear.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-app-view-composite-view-linear.css"]
  }; }
  static get properties() { return {
    "appConfig": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "AppConfig",
        "resolved": "AppConfig",
        "references": {
          "AppConfig": {
            "location": "import",
            "path": "@dvn/core/models/app-config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "user": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "User",
        "resolved": "User",
        "references": {
          "User": {
            "location": "import",
            "path": "@dvn/kimlik/api/models"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "userSettings": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "UserSettings",
        "resolved": "UserSettings",
        "references": {
          "UserSettings": {
            "location": "import",
            "path": "@dvn/kimlik/api/models/UserSettings"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "membership": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Membership",
        "resolved": "Membership",
        "references": {
          "Membership": {
            "location": "import",
            "path": "@dvn/core/models/membership"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "apiAtama": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "AtamaApi",
        "resolved": "AtamaApi",
        "references": {
          "AtamaApi": {
            "location": "import",
            "path": "../../../../../atama/src/api/atamaApi"
          }
        }
      },
      "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": ""
      }
    },
    "apiTakvim": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "TakvimApi",
        "resolved": "TakvimApi",
        "references": {
          "TakvimApi": {
            "location": "import",
            "path": "@dvn/takvim/api/takvimApi"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "apiUye": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "UyeApi",
        "resolved": "UyeApi",
        "references": {
          "UyeApi": {
            "location": "import",
            "path": "@dvn/uye/api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "appContextInvalidator": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "AppContextInvalidator",
        "resolved": "() => void",
        "references": {
          "AppContextInvalidator": {
            "location": "import",
            "path": "@dvn/core/global/context/context"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "logger": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Logger",
        "resolved": "Logger",
        "references": {
          "Logger": {
            "location": "import",
            "path": "@dvn/core/log/logger"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "navigate": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "NavigationHandler",
        "resolved": "(config: NavigateConfig) => void",
        "references": {
          "NavigationHandler": {
            "location": "import",
            "path": "@dvn/core/routing/routing"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "notifier": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Notifier",
        "resolved": "Notifier",
        "references": {
          "Notifier": {
            "location": "import",
            "path": "@dvn/alaca/global/notifier/models"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "view": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "CompositeViewLinearConfig",
        "resolved": "CompositeViewLinearConfig",
        "references": {
          "CompositeViewLinearConfig": {
            "location": "import",
            "path": "@dvn/core/components/views/dvn-app-view-composite-view-linear/composite-view-linear-config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "params": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "params",
      "reflect": false
    }
  }; }
  static get states() { return {
    "itemFetchState": {},
    "needsToFetchData": {}
  }; }
  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 elementRef() { return "el"; }
  static get listeners() { return [{
      "name": "invalidation",
      "method": "onInvalidation",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
