"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ItemActionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _ConfirmModalBIReporter = require("./ConfirmModalBIReporter");
class ItemActionsState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "keyedItem", void 0);
    (0, _defineProperty2.default)(this, "fade", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "biReporter", void 0);
    (0, _defineProperty2.default)(this, "confirmModal", new _bexCore.ConditionalModalState());
    (0, _defineProperty2.default)(this, "actionCellAPI", void 0);
    this.fade = params.fade;
    this.keyedItem = params.keyedItem;
    this.collection = params.collection;
    this.biReporter = params.reportBi ? new _ConfirmModalBIReporter.ConfirmModalBIReporter({
      reportBi: params.reportBi,
      itemId: params.keyedItem.id,
      itemIndex: params.keyedItem.indexWithinPage
    }) : null;
    (0, _mobx.makeObservable)(this, {
      keyedItem: _mobx.observable.ref,
      fade: _mobx.observable.ref,
      onFadeStart: _mobx.action.bound,
      onFadeDone: _mobx.action.bound,
      setItemIfChanged: _mobx.action,
      openConfirmModal: _mobx.action.bound,
      openConfirmDeleteModal: _mobx.action.bound
    });
    this.actionCellAPI = {
      openConfirmModal: this.openConfirmModal,
      openConfirmDeleteModal: this.openConfirmDeleteModal,
      collection: this.collection,
      keyedItem: this.keyedItem
    };
  }
  openConfirmModal(props) {
    const {
      translate: t
    } = this.collection;
    this.confirmModal.open({
      title: t('cairo.confirmModal.title'),
      primaryButtonText: t('cairo.confirm.button'),
      secondaryButtonText: t('cairo.cancel.button'),
      ...props
    });
  }
  openConfirmDeleteModal(props) {
    const {
      translate: t
    } = this.collection;
    if (!this.confirmModal.isOpen) {
      var _this$biReporter;
      (_this$biReporter = this.biReporter) == null || _this$biReporter.reportDeleteModalShown();
    }
    this.openConfirmModal({
      theme: 'destructive',
      title: t('cairo.deleteModal.title'),
      content: t('cairo.deleteModal.body'),
      ...props,
      primaryButtonOnClick: () => {
        var _this$biReporter2;
        (_this$biReporter2 = this.biReporter) == null || _this$biReporter2.reportDeleteModalCtaClicked('Delete');
        props == null || props.primaryButtonOnClick == null || props.primaryButtonOnClick();
      },
      secondaryButtonOnClick: () => {
        var _this$biReporter3;
        (_this$biReporter3 = this.biReporter) == null || _this$biReporter3.reportDeleteModalCtaClicked('Cancel');
        props == null || props.secondaryButtonOnClick == null || props.secondaryButtonOnClick();
      },
      onCloseButtonClick: e => {
        var _this$biReporter4;
        (_this$biReporter4 = this.biReporter) == null || _this$biReporter4.reportDeleteModalCtaClicked('X');
        props == null || props.onCloseButtonClick == null || props.onCloseButtonClick(e);
      }
    });
  }
  onFadeStart() {
    if (this.fade === 'exit') {
      this.fade = 'exiting';
    }
  }
  onFadeDone() {
    const {
      fade
    } = this;
    if (fade === 'exiting') {
      this.fade = 'idle';
    }
  }
  setItemIfChanged(keyedItem) {
    if (this.keyedItem !== keyedItem) {
      this.keyedItem = keyedItem;
    }
  }
}
exports.ItemActionsState = ItemActionsState;
//# sourceMappingURL=ItemActionsState.js.map