"use strict";

exports.__esModule = true;
exports.useFixMissingContactsViews = useFixMissingContactsViews;
var _react = require("react");
var _react2 = require("@wix/bex-core/react");
var _http = require("@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/http");
var _types = require("@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/types");
function useFixMissingContactsViews(collectionState) {
  const {
    errorMonitor,
    httpClient
  } = (0, _react2.useWixPatternsContainer)();
  (0, _react.useState)(() => {
    const oldContactsPageId = 'contacts-page-component';
    collectionState._fixMissingContactsViews = async state => {
      const {
        viewsService
      } = state;
      const {
        viewsStorage,
        fqdn
      } = viewsService;
      try {
        var _data$settings, _data$settings2, _oldViews$views, _newViews$views, _oldViews$views2;
        const {
          data
        } = await httpClient.request((0, _http.getGlobalSettings)({
          host: _types.Host.BUSINESS_MANAGER
        }));
        const isMigrated = !!(data != null && (_data$settings = data.settings) != null && (_data$settings = _data$settings[oldContactsPageId]) != null && _data$settings._migrationDate);
        if (isMigrated) {
          return;
        }
        const oldViews = data == null || (_data$settings2 = data.settings) == null || (_data$settings2 = _data$settings2[oldContactsPageId]) == null || (_data$settings2 = _data$settings2.cairo) == null ? void 0 : _data$settings2[fqdn];

        // no views from old contacts views app-settings
        if (oldViews == null || !(oldViews != null && (_oldViews$views = oldViews.views) != null && _oldViews$views.list)) {
          return;
        }
        const newViews = await viewsStorage.getItem(fqdn);
        const newViewsList = (newViews == null || (_newViews$views = newViews.views) == null ? void 0 : _newViews$views.list) || {};
        const oldViewsList = oldViews == null || (_oldViews$views2 = oldViews.views) == null ? void 0 : _oldViews$views2.list;
        const newViewNames = new Set(Object.values(newViewsList).map(view => view == null ? void 0 : view.name).filter(n => n));
        Object.values(oldViewsList).forEach(oldView => {
          if (oldView != null && oldView.id && !newViewNames.has(oldView == null ? void 0 : oldView.name)) {
            newViewsList[oldView == null ? void 0 : oldView.id] = oldView;
          }
        });
        if (Object.keys(newViewsList).length > 0) {
          await Promise.all([viewsStorage.setItem(fqdn, {
            views: {
              list: newViewsList
            }
          }), httpClient.request((0, _http.setGlobalSettings)({
            host: _types.Host.BUSINESS_MANAGER,
            settings: {
              [oldContactsPageId]: {
                _migrationDate: new Date()
              }
            }
          }))]);
        }
      } catch (e) {
        errorMonitor.captureException(e);
      }
    };
  });
}
//# sourceMappingURL=useFixMissingContactsViews.js.map