"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionDataExtensionState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _bi = require("@wix/bex-core/bi");
var _CustomFieldFilters = require("../CustomFieldFilters");
var _CollectionToolbarFilters = require("../CollectionToolbarFilters");
var _mobx = require("mobx");
var _CustomFieldsPanelState = require("../../state/CustomFields/CustomFieldsPanelState");
class CollectionDataExtensionState {
  get featuredPageActions() {
    return [this.DataExtension.manageCustomFieldsPageAction];
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "dataExtension", void 0);
    (0, _defineProperty2.default)(this, "customFieldsPanel", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "DataExtension", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "components", void 0);
    (0, _defineProperty2.default)(this, "props", void 0);
    this.toolbar = params.toolbar;
    this.props = params.props;
    this.DataExtension = params.DataExtension;
    this.dataExtension = new _bexCore.DataExtensionState({
      container: params.container,
      fqdn: params.toolbar.collection.fqdn ?? {
        product: params.toolbar.collection.queryName
      },
      closeSidePanel: params.toolbar.closeSidePanel,
      disableUserDefinedWriting: params.disableUserDefinedWriting,
      filterable: params.filterable,
      containsPii: params.containsPii,
      defaultPermissions: params.defaultPermissions,
      enableArchive: params.enableArchive
    });
    this.customFieldsPanel = new _CustomFieldsPanelState.CustomFieldsPanelState({
      dataExtension: this.dataExtension,
      reportBi: this.toolbar.reportBi,
      closeSidePanel: this.toolbar.closeSidePanel
    });
    this.components = {
      CollectionToolbarFilters: _CollectionToolbarFilters.CollectionToolbarFilters
    };
  }
  _getFetchSchemasError(err) {
    return new _bexCore.WixPatternsError({
      errorCode: 'InitCustomFieldsDataExtensionFailed',
      originalException: err
    });
  }
  _handleFetchSchemasError() {
    var _this$dataExtension$c, _this$dataExtension$c2;
    const error = this.dataExtension.initTask.status.error;
    if (!error) {
      return;
    }
    const cairoError = this._getFetchSchemasError(error);
    (_this$dataExtension$c = (_this$dataExtension$c2 = this.dataExtension.container).showToast) == null || _this$dataExtension$c.call(_this$dataExtension$c2, {
      message: this.dataExtension.container.translate('cairo.customFields.fetch.technicalError'),
      action: {
        uiType: 'LINK',
        text: this.dataExtension.container.translate('cairo.toast.retry'),
        onClick: () => {
          this.toolbar.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoTryAgainClicked)({
            actionName: 'Fetch extended fields',
            loaction: 'toast'
          }));
          this.dataExtension.fetchSchemas().catch(this._handleFetchSchemasError.bind(this));
        },
        removeToastOnClick: true
      },
      type: 'WARNING',
      biName: 'cairo-fetch-schemas-error-toast'
    });
    this.dataExtension.container.internalMonitor.reportError({
      error: cairoError
    });
    this.toolbar.reportBi((0, _bi.cairoErrorInLoadingAComponent)({
      errorType: cairoError.name
    }));
  }
  _addCustomFieldFilters() {
    const customFieldFilter = {
      date: _bexCore.dateRangeFilter,
      dateTime: _bexCore.dateRangeFilter,
      checkbox: _bexCore.idNameArrayFilter,
      decimal: _bexCore.numberRangeFilter,
      integer: _bexCore.numberRangeFilter,
      shortText: _bexCore.stringOperatorFilter,
      url: _bexCore.stringOperatorFilter,
      longText: _bexCore.stringOperatorFilter
    };
    this.dataExtension.filterableCustomFields.forEach(customField => {
      const filter = customFieldFilter[customField.type];
      if (!filter) {
        return;
      }
      this.toolbar.collection.query.addFilterIfNeeded(customField.id, filter({
        name: customField.id,
        isCustomField: true
      }));
    });
  }
  init() {
    const {
      initTask,
      dataExtension,
      toolbar
    } = this;
    const {
      customColumnsState
    } = toolbar;
    const disposers = [dataExtension.init(), (0, _mobx.reaction)(() => this.dataExtension.filterableCustomFields, () => this._addCustomFieldFilters()), ...(customColumnsState != null && customColumnsState.events ? [(0, _bexCore.addEventListener)(customColumnsState.events, 'beforeInitialFetch', () => initTask.status.promise)] : [])];
    initTask.runOnce(async () => {
      var _dataExtension$initTa;
      dataExtension.initTask.runOnce();
      dataExtension.container.initTask.runOnce();
      await Promise.all([(_dataExtension$initTa = dataExtension.initTask.status.promise) == null ? void 0 : _dataExtension$initTa.catch(() => {}), dataExtension.container.initTask.status.promise]);
      this._handleFetchSchemasError();
      toolbar.setNewExtendedColumns(dataExtension.availableCustomFields, {
        defaultHidden: true
      });
    });
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
  buildCustomFieldFilterWrapper(customField) {
    return (0, _CustomFieldFilters.buildCustomFieldFilterElement)(this.toolbar, customField) || undefined;
  }
  buildCustomFieldFilters() {
    return this.dataExtension.filterableCustomFields.map(customField => this.buildCustomFieldFilterWrapper(customField)).filter(Boolean);
  }
}
exports.CollectionDataExtensionState = CollectionDataExtensionState;
//# sourceMappingURL=CollectionDataExtensionState.js.map