"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ViewsService = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
class ViewsService {
  constructor(params) {
    (0, _defineProperty2.default)(this, "viewsStorage", void 0);
    (0, _defineProperty2.default)(this, "defaultViewStorage", void 0);
    (0, _defineProperty2.default)(this, "fqdn", void 0);
    (0, _defineProperty2.default)(this, "monitor", void 0);
    (0, _defineProperty2.default)(this, "errorHandler", void 0);
    (0, _defineProperty2.default)(this, "translate", void 0);
    this.viewsStorage = params.viewsStorage;
    this.defaultViewStorage = params.defaultViewStorage;
    this.fqdn = params.fqdn;
    this.monitor = params.monitor;
    this.errorHandler = params.errorHandler;
    this.translate = params.translate;
  }
  async migrateFromLocalStorage() {
    const {
      viewsStorage,
      fqdn
    } = this;
    try {
      await (viewsStorage.migrateItem == null ? void 0 : viewsStorage.migrateItem(fqdn));
    } catch (e) {
      this.monitor.reportError({
        error: e
      });
    }
  }
  async fetchViews() {
    const {
      viewsStorage,
      fqdn,
      errorHandler
    } = this;
    const data = await errorHandler.withErrorHandler(() => viewsStorage.getItem(fqdn).catch(e => {
      if ((e == null ? void 0 : e.message) === 'Key was not found in capsule') {
        return null;
      }
      throw e;
    }), {});
    if ((data == null ? void 0 : data.views) == null) {
      return null;
    }
    const {
      list,
      hiddenPresetIds
    } = data.views;
    return {
      views: list && Object.values(list).filter(view => view != null).sort((a, b) => new Date(a.createdAt ?? 0).valueOf() - new Date(b.createdAt ?? 0).valueOf()),
      hiddenPresetIds: hiddenPresetIds && Object.values(hiddenPresetIds).filter(id => id != null)
    };
  }
  async fetchDefaultViewId() {
    const {
      defaultViewStorage,
      fqdn,
      errorHandler
    } = this;
    const data = await errorHandler.withErrorHandler(() => defaultViewStorage.getItem(`${fqdn}.defaultViewId`).catch(e => {
      if ((e == null ? void 0 : e.message) === 'Key was not found in capsule') {
        return null;
      }
      throw e;
    }), {});
    return typeof data === 'string' ? data : (data == null ? void 0 : data.id) ?? null;
  }
  async saveView(view, props) {
    const {
      viewsStorage,
      fqdn,
      errorHandler,
      translate: t
    } = this;
    await errorHandler.withErrorHandler(() => viewsStorage.setItem(fqdn, {
      views: {
        list: {
          [view.id]: {
            ...view,
            createdAt: new Date().toISOString()
          }
        }
      }
    }), {}, {
      statusCodesOverrides: {
        INTERNAL: () => ({
          message: props != null && props.isRename ? t('cairo.manageView.Rename.error.toast') : t('cairo.manageView.saveView.error.toast')
        })
      }
    });
  }
  async deleteView(view) {
    const {
      viewsStorage,
      fqdn,
      errorHandler,
      translate: t
    } = this;
    await errorHandler.withErrorHandler(() => viewsStorage.setItem(fqdn, {
      views: {
        list: {
          [view.id]: null
        }
      }
    }), {}, {
      statusCodesOverrides: {
        INTERNAL: () => ({
          message: t('cairo.manageView.Delete.CTA.error.toast')
        })
      }
    });
  }
  async updateHiddenPreset(idToDelete) {
    const {
      viewsStorage,
      fqdn,
      errorHandler,
      translate: t
    } = this;
    await errorHandler.withErrorHandler(() => viewsStorage.setItem(fqdn, {
      views: {
        hiddenPresetIds: {
          [idToDelete]: idToDelete
        }
      }
    }), {}, {
      statusCodesOverrides: {
        INTERNAL: () => ({
          message: t('cairo.manageView.Delete.CTA.error.toast')
        })
      }
    });
  }
  async updateDefaultView(view) {
    const {
      defaultViewStorage,
      fqdn,
      errorHandler,
      translate: t
    } = this;
    await errorHandler.withErrorHandler(async () => {
      const defaultViewKey = `${fqdn}.defaultViewId`;
      if (view != null) {
        await defaultViewStorage.setItem(defaultViewKey, {
          id: view.id
        });
      } else {
        await defaultViewStorage.removeItem(defaultViewKey);
      }
    }, {}, {
      statusCodesOverrides: {
        INTERNAL: view ? () => ({
          message: t('cairo.manageView.setAsDefault.error.toast', {
            viewName: view.name
          })
        }) : undefined
      }
    });
  }
}
exports.ViewsService = ViewsService;
//# sourceMappingURL=viewsService.js.map