import { r as registerInstance, c as createEvent, h, H as Host } from './index-ecdfb761.js';
import { I as ItemDataProvider } from './data-provider-e779160f.js';
import { c as convertValues } from './action-util-f72a13be.js';
import { g as generateFieldModelFromRawData } from './FieldModelGenerator-378c8613.js';
import { S as SingleChoiceField } from './index-23baeeef.js';
import { I as InvalidationAction, N as NotificationAction } from './actions-b9a0f808.js';
import { I as ItemFetchState } from './item-fetch-state-a8a04fb7.js';
import { f as findField } from './field-5a687ea4.js';
import './data-model-88af49fc.js';
import './filter-cbaa3d7d.js';
import './filter-8542e288.js';
import './reference-field-a5b414d1.js';
import './BaseField-0b01dc2c.js';
import './index-61dcd269.js';
import './FieldGenerator-3ae403b2.js';
import './date-1bc489bd.js';
import './index-4e451db4.js';
import './index-09efdc01.js';
import './index-150b6a59.js';

const dvnAppViewFormViewCss = ".sc-dvn-app-view-form-view-h{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1;padding:calc(4 * var(--spacing-unit));-ms-flex-direction:column;flex-direction:column;overflow:auto}.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 if (value) {
      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 };
