import { r as registerInstance, c as createEvent, h, H as Host } from './index-acdc1ac0.js';
import './index-d9d18e9c.js';
import './data-model-bb5789f5.js';
import './BaseField-d94c5b14.js';
import { g as generateFieldModelFromRawData } from './FieldModelGenerator-755e3a85.js';
import './date-b270be76.js';
import { S as SingleChoiceField } from './index-a95fa2f4.js';
import './filter-cbaa3d7d.js';
import { I as InvalidationAction, N as NotificationAction } from './actions-b9a0f808.js';
import { I as ItemFetchState } from './item-fetch-state-3ee8988a.js';
import { f as findField } from './field-67ec102a.js';
import './filter-8542e288.js';
import { I as ItemDataProvider } from './data-provider-3b86770f.js';
import { c as convertValues } from './action-util-97f44c92.js';

const dvnAppViewFormViewCss = ".sc-dvn-app-view-form-view-h{display:flex;flex:1;padding:calc(4 * var(--spacing-unit));flex-direction:column}.sc-dvn-app-view-form-view-h .progress-container.sc-dvn-app-view-form-view{padding:calc(4 * var(--spacing-unit))}.sc-dvn-app-view-form-view-h h3.sc-dvn-app-view-form-view{margin:0 0 calc(6 * var(--spacing-unit))}";

function getFieldFromFormFieldConfig(fields, fieldData) {
  return fieldData.field || findField(fields, fieldData.fieldId);
}
// sets value if the parameters contain value for fields
const prepareFields = (view, params = {}) => {
  const data = convertValues(view.conversionConfig, params);
  return view.ui.fields.map(formFieldConfig => {
    var _a;
    const field = generateFieldModelFromRawData(getFieldFromFormFieldConfig(view.table.fields, formFieldConfig));
    const value = data[field.id];
    if (value && field.type === SingleChoiceField.type) {
      const singleChoice = field;
      if (singleChoice.dataProvider) {
        // dvn-select doesn't show any data if it has a data provider (meaning that the options are not set)
        // we need to pass initial options with the selected value in it
        singleChoice.dataProvider.setInitialData([value]);
      }
      field.setValue(((_a = singleChoice.dataAdapter) === null || _a === void 0 ? void 0 : _a.value(value)) || value);
    }
    else {
      field.setValue(value);
    }
    return field;
  });
};
const DvnAppViewFormView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.action = createEvent(this, "action", 7);
    this.invalidation = createEvent(this, "invalidation", 7);
    this.notification = createEvent(this, "notification", 7);
    this.isProgressing = false;
    this.itemFetchState = ItemFetchState.init();
    this.onSubmit = (event) => {
      if (!this.view.handler.onSubmit) {
        return;
      }
      this.isProgressing = true;
      this.formErrors = undefined;
      this.view.handler.onSubmit(event).then(result => {
        this.isProgressing = false;
        if (result === null || result === void 0 ? void 0 : result.events) {
          result.events.forEach(eventToEmit => {
            if (eventToEmit instanceof InvalidationAction) {
              this.invalidation.emit(eventToEmit);
            }
            if (eventToEmit instanceof NotificationAction) {
              this.notification.emit(eventToEmit);
            }
          });
        }
        if (result === null || result === void 0 ? void 0 : result.navigation) {
          this.action.emit(result.navigation);
        }
      }).catch(errors => {
        this.isProgressing = false;
        this.formErrors = errors;
      });
    };
  }
  componentWillLoad() {
    var _a;
    const { apiData, params, view } = this;
    const { query, table } = view;
    if (query) {
      const queryParams = { itemId: params.itemId };
      // fetch the initial data if the query is provided
      this.itemDataProvider = new ItemDataProvider(apiData, queryParams, table, query, query.fields, (itemFetchState) => {
        this.itemFetchState = itemFetchState;
      });
      (_a = this.itemDataProvider) === null || _a === void 0 ? void 0 : _a.fetchData(this.itemFetchState);
    }
  }
  // needed for invalidation
  componentDidUpdate() {
    var _a;
    const { itemFetchState } = this;
    if (!itemFetchState.data && !itemFetchState.isPending && !itemFetchState.error) {
      (_a = this.itemDataProvider) === null || _a === void 0 ? void 0 : _a.fetchData(this.itemFetchState);
    }
  }
  async invalidate(_event) {
    var _a;
    (_a = this.itemDataProvider) === null || _a === void 0 ? void 0 : _a.fetchData(this.itemFetchState);
  }
  ;
  render() {
    const { view, params, formErrors, isProgressing } = this;
    const { i18n = {} } = view.ui;
    if (!view.query) {
      // show the form without any fetch related ui
      return (h(Host, null, h("h3", null, view.title), h("doldurma-form", { fields: prepareFields(view, params), onSubmit: this.onSubmit, submitButtonLabel: i18n.submit || 'Submit', progressing: isProgressing, formErrors: formErrors })));
    }
    const { itemFetchState } = this;
    return (h(Host, null, (!itemFetchState.isPending && !itemFetchState.error) &&
      h("h3", null, view.title), (!itemFetchState.isPending && !itemFetchState.error) &&
      h("doldurma-form", { fields: prepareFields(view, Object.assign(Object.assign({}, (params)), { item: itemFetchState.data })), onSubmit: this.onSubmit, submitButtonLabel: i18n.submit || 'Submit', progressing: isProgressing, formErrors: formErrors }), 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" }, i18n.fetchError || 'Fetching data failed.', h("dvn-button", { onClick: () => { var _a; return (_a = this.itemDataProvider) === null || _a === void 0 ? void 0 : _a.fetchData(this.itemFetchState); } }, i18n.retry))));
  }
};
DvnAppViewFormView.style = dvnAppViewFormViewCss;

export { DvnAppViewFormView as dvn_app_view_form_view };
