'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-36b3af04.js');
const dataProvider = require('./data-provider-1eb24deb.js');
const actionUtil = require('./action-util-8b538079.js');
const FieldModelGenerator = require('./FieldModelGenerator-205dd99c.js');
const index$1 = require('./index-fe19a98b.js');
const actions = require('./actions-d7eabd95.js');
const itemFetchState = require('./item-fetch-state-72f8fa7c.js');
const field = require('./field-b81617b0.js');
require('./index-a8f1783c.js');
require('./BaseField-d019ade6.js');
require('./data-model-c51de090.js');
require('./filter-d5040f65.js');
require('./filter-a24b968a.js');
require('./index-74a00556.js');
require('./date-25b104f6.js');
require('./index-ae305bae.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 || field.findField(fields, fieldData.fieldId);
}
// sets value if the parameters contain value for fields
const prepareFields = (view, params = {}) => {
  const data = actionUtil.convertValues(view.conversionConfig, params);
  return view.ui.fields.map(formFieldConfig => {
    var _a;
    const field = FieldModelGenerator.generateFieldModelFromRawData(getFieldFromFormFieldConfig(view.table.fields, formFieldConfig));
    const value = data[field.id];
    if (value && field.type === index$1.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) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.invalidation = index.createEvent(this, "invalidation", 7);
    this.notification = index.createEvent(this, "notification", 7);
    this.isProgressing = false;
    this.itemFetchState = 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 actions.InvalidationAction) {
              this.invalidation.emit(eventToEmit);
            }
            if (eventToEmit instanceof actions.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 dataProvider.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 (index.h(index.Host, null, index.h("h3", null, view.title), index.h("doldurma-form", { fields: prepareFields(view, params), onSubmit: this.onSubmit, submitButtonLabel: i18n.submit || 'Submit', progressing: isProgressing, formErrors: formErrors })));
    }
    const { itemFetchState } = this;
    return (index.h(index.Host, null, (!itemFetchState.isPending && !itemFetchState.error) &&
      index.h("h3", null, view.title), (!itemFetchState.isPending && !itemFetchState.error) &&
      index.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 &&
      index.h("div", { class: "d-flex flex-grow-1 align-items-center justify-content-center detail-view-progress-container" }, index.h("dvn-progress", { indefinite: true, tint: "primary" })), itemFetchState.error &&
      index.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.', index.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;

exports.dvn_app_view_form_view = DvnAppViewFormView;
