"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CustomColumnsHistoryState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
function parseSelectedColumnsString(selectedColumnsFromUrl) {
  return selectedColumnsFromUrl.split(',').map(columnExpression => {
    const [id = '', isSelected] = columnExpression.split(' ');
    return {
      id: decodeURIComponent(id),
      isSelected: isSelected !== 'false'
    };
  }).filter(({
    id
  }) => id);
}

// Legacy `selectedColumns` stays bound to non-spreadsheet layouts so existing
// deep links keep their meaning; spreadsheet uses a sibling param.
const SELECTED_COLUMNS_PARAM = 'selectedColumns';
const SPREADSHEET_SELECTED_COLUMNS_PARAM = 'spreadsheetSelectedColumns';
const getSelectedColumnsParamForLayout = layout => layout === 'spreadsheet' ? SPREADSHEET_SELECTED_COLUMNS_PARAM : SELECTED_COLUMNS_PARAM;
const serializeSelectedColumns = selectedColumns => selectedColumns.map(c => `${encodeURIComponent(c.id)}${!c.isSelected ? ' false' : ''}`).join(',');
class CustomColumnsHistoryState {
  get selectedColumnsSearchParamName() {
    var _this$customColumns$t;
    return getSelectedColumnsParamForLayout((_this$customColumns$t = this.customColumns.table.layoutState) == null ? void 0 : _this$customColumns$t.viewType);
  }

  // Live, per-layout read — unlike `_selectedColumnsFromUrl` (frozen at the
  // initial layout), this reflects the active layout after a switch so URL
  // takes precedence over dataCapsule per layout, not just on first load.
  get hasUrlColumnsForCurrentLayout() {
    const params = new URLSearchParams(this.history.location.search);
    return !!params.get(this.selectedColumnsSearchParamName);
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "customColumns", void 0);
    (0, _defineProperty2.default)(this, "history", void 0);
    (0, _defineProperty2.default)(this, "_selectedColumnsFromUrl", void 0);
    this.customColumns = params.customColumns;
    this.history = params.history;
    this._preInitHistoryPersistence();
  }
  _preInitHistoryPersistence() {
    const {
      history,
      customColumns
    } = this;
    const searchParams = new URLSearchParams(history.location.search);
    this._selectedColumnsFromUrl = searchParams.get(this.selectedColumnsSearchParamName);
    if (this._selectedColumnsFromUrl) {
      this.customColumns.table.selectedColumns.setValue(parseSelectedColumnsString(this._selectedColumnsFromUrl));
      customColumns.table.multi.collections.forEach(({
        query
      }) => {
        query._initializedFromHistory = true;
      });
    }
  }
  init() {
    const disposers = [(0, _mobx.reaction)(() => this.customColumns.table.selectedColumns.value, selectedColumns => {
      const paramName = this.selectedColumnsSearchParamName;
      (0, _bexCore.replaceSearchParamsWith)(this.history, searchParams => {
        if (selectedColumns.length) {
          searchParams.set(paramName, serializeSelectedColumns(selectedColumns));
        } else {
          searchParams.delete(paramName);
        }
      });
    }, {
      fireImmediately: true,
      delay: 250
    }), (0, _mobx.reaction)(() => {
      var _this$customColumns$t2;
      return (_this$customColumns$t2 = this.customColumns.table.layoutState) == null ? void 0 : _this$customColumns$t2.viewType;
    }, (newLayout, prevLayout) => {
      if (!newLayout || !prevLayout || newLayout === prevLayout) {
        return;
      }
      if (this.customColumns.table._cacheRestoredLayout === newLayout) {
        return;
      }
      // Views path is handled by CollectionViewsState's own reaction.
      if (this.customColumns.table.viewsProps) {
        return;
      }
      const searchParams = new URLSearchParams(this.history.location.search);
      const value = searchParams.get(getSelectedColumnsParamForLayout(newLayout));
      if (value) {
        this.customColumns.table.selectedColumns.setValue(parseSelectedColumnsString(value));
      } else {
        this.customColumns.table.selectedColumns.reset();
      }
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.CustomColumnsHistoryState = CustomColumnsHistoryState;
//# sourceMappingURL=CustomColumnsHistoryState.js.map