import { r as registerInstance, h, H as Host, g as getElement } from './index-b7735e0d.js';
import { I as ItemDataProvider } from './data-provider-d3b64335.js';
import { c as convertValues } from './action-util-f3f0e010.js';
import { I as ItemFetchState } from './item-fetch-state-610938e5.js';
import './data-model-f7ccf826.js';
import './filter-cbaa3d7d.js';
import './filter-8542e288.js';

const dvnAppViewCompositeViewLinearCss = "dvn-app-view-composite-view-linear{margin-top:calc(4 * var(--spacing-unit));margin-bottom:calc(4 * var(--spacing-unit));--composite-view-gutter:calc(4 * var(--spacing-unit));--weigh:1;display:-ms-flexbox;display:flex}dvn-app-view-composite-view-linear .view-wrappers-container{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1}dvn-app-view-composite-view-linear.horizontal>.view-wrappers-container>.view-wrapper{display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;-ms-flex:var(--weigh);flex:var(--weigh);margin-right:var(--composite-view-gutter)}dvn-app-view-composite-view-linear.horizontal>.view-wrappers-container>.view-wrapper:last-child{margin-right:0}dvn-app-view-composite-view-linear.vertical>.view-wrappers-container>.view-wrapper{display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;margin-bottom:var(--composite-view-gutter)}";

function getViewComponentName(view) {
  return `dvn-app-view-${view.type}`;
}
let DvnAppViewCompositeViewLinear = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    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,
      apiSozluk: this.apiSozluk,
      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"))));
  }
  get el() { return getElement(this); }
};
DvnAppViewCompositeViewLinear.style = dvnAppViewCompositeViewLinearCss;

export { DvnAppViewCompositeViewLinear as dvn_app_view_composite_view_linear };
