"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.FieldActionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
class FieldActionsState {
  constructor(translate, handlers, permissions, showToast, refresh) {
    (0, _defineProperty2.default)(this, "handlers", void 0);
    (0, _defineProperty2.default)(this, "permissions", void 0);
    (0, _defineProperty2.default)(this, "translate", void 0);
    (0, _defineProperty2.default)(this, "showToast", void 0);
    (0, _defineProperty2.default)(this, "_refresh", void 0);
    (0, _defineProperty2.default)(this, "_currentToastRemove", void 0);
    (0, _defineProperty2.default)(this, "confirmModal", new _bexCore.ConditionalModalState());
    (0, _defineProperty2.default)(this, "deleteTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "makePrimaryTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "duplicateTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "duplicateWithContentTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "_showToast", config => {
      var _this$_currentToastRe;
      if (!this.showToast) {
        return;
      }
      (_this$_currentToastRe = this._currentToastRemove) == null || _this$_currentToastRe.call(this);
      this._currentToastRemove = this.showToast(config).remove;
    });
    (0, _defineProperty2.default)(this, "_withRefresh", async operation => {
      var _this$_refresh;
      await operation();
      await ((_this$_refresh = this._refresh) == null ? void 0 : _this$_refresh.call(this));
    });
    (0, _defineProperty2.default)(this, "_runDelete", async (fieldId, deleteField) => {
      await this.deleteTask.run(() => this._withRefresh(() => deleteField(fieldId)));
      if (this.deleteTask.status.isSuccess) {
        this.confirmModal.close();
        this._showToast({
          type: 'SUCCESS',
          message: 'Field deleted',
          biName: 'cairo-field-action-delete-success'
        });
      } else if (this.deleteTask.status.isError) {
        this._showToast({
          type: 'ERROR',
          message: "Couldn't delete field",
          biName: 'cairo-field-action-delete-error',
          action: {
            text: 'Try again',
            onClick: () => this._runDelete(fieldId, deleteField),
            removeToastOnClick: true
          }
        });
      }
    });
    this.translate = translate;
    this.handlers = handlers;
    this.permissions = permissions;
    this.showToast = showToast;
    this._refresh = refresh;
    (0, _mobx.makeObservable)(this, {
      handlers: _mobx.observable.ref,
      permissions: _mobx.observable.ref,
      setHandlers: _mobx.action,
      setPermissions: _mobx.action,
      setRefresh: _mobx.action,
      confirmDelete: _mobx.action,
      makePrimary: _mobx.action,
      duplicate: _mobx.action,
      duplicateWithContent: _mobx.action,
      isMakingPrimary: _mobx.computed,
      isDuplicating: _mobx.computed,
      isDuplicatingWithContent: _mobx.computed
    });
  }
  setHandlers(handlers) {
    this.handlers = handlers;
  }
  setPermissions(permissions) {
    this.permissions = permissions;
  }
  setRefresh(refresh) {
    this._refresh = refresh;
  }
  get isMakingPrimary() {
    return this.makePrimaryTask.status.isLoading;
  }
  get isDuplicating() {
    return this.duplicateTask.status.isLoading;
  }
  get isDuplicatingWithContent() {
    return this.duplicateWithContentTask.status.isLoading;
  }
  confirmDelete(fieldId) {
    var _this$handlers;
    const deleteField = (_this$handlers = this.handlers) == null ? void 0 : _this$handlers.deleteField;
    if (!deleteField) {
      return;
    }
    const t = this.translate;
    this.confirmModal.open({
      theme: 'destructive',
      title: t('cairo.deleteModal.title'),
      content: t('cairo.deleteModal.body'),
      primaryButtonText: t('cairo.delete.button'),
      secondaryButtonText: t('cairo.cancel.button'),
      primaryButtonOnClick: () => {
        this._runDelete(fieldId, deleteField);
      }
    });
  }
  async makePrimary(fieldId) {
    var _this$handlers2;
    const makePrimary = (_this$handlers2 = this.handlers) == null ? void 0 : _this$handlers2.makePrimary;
    if (!makePrimary || this.makePrimaryTask.status.isLoading) {
      return;
    }
    await this.makePrimaryTask.run(() => this._withRefresh(() => makePrimary(fieldId)));
    if (this.makePrimaryTask.status.isSuccess) {
      this._showToast({
        type: 'SUCCESS',
        message: 'Primary field updated',
        biName: 'cairo-field-action-make-primary-success'
      });
    } else if (this.makePrimaryTask.status.isError) {
      this._showToast({
        type: 'ERROR',
        message: "Couldn't update primary field",
        biName: 'cairo-field-action-make-primary-error',
        action: {
          text: 'Try again',
          onClick: () => this.makePrimary(fieldId),
          removeToastOnClick: true
        }
      });
    }
  }
  async duplicate(fieldId) {
    var _this$handlers3;
    const duplicateField = (_this$handlers3 = this.handlers) == null ? void 0 : _this$handlers3.duplicateField;
    if (!duplicateField || this.duplicateTask.status.isLoading) {
      return;
    }
    await this.duplicateTask.run(() => this._withRefresh(() => duplicateField(fieldId)));
    if (this.duplicateTask.status.isSuccess) {
      this._showToast({
        type: 'SUCCESS',
        message: 'Field duplicated',
        biName: 'cairo-field-action-duplicate-success'
      });
    } else if (this.duplicateTask.status.isError) {
      this._showToast({
        type: 'ERROR',
        message: "Couldn't duplicate field",
        biName: 'cairo-field-action-duplicate-error',
        action: {
          text: 'Try again',
          onClick: () => this.duplicate(fieldId),
          removeToastOnClick: true
        }
      });
    }
  }
  async duplicateWithContent(fieldId) {
    var _this$handlers4;
    const duplicateFieldWithContent = (_this$handlers4 = this.handlers) == null ? void 0 : _this$handlers4.duplicateFieldWithContent;
    if (!duplicateFieldWithContent || this.duplicateWithContentTask.status.isLoading) {
      return;
    }
    await this.duplicateWithContentTask.run(() => this._withRefresh(() => duplicateFieldWithContent(fieldId)));
    if (this.duplicateWithContentTask.status.isSuccess) {
      this._showToast({
        type: 'SUCCESS',
        message: 'Field duplicated with content',
        biName: 'cairo-field-action-duplicate-with-content-success'
      });
    } else if (this.duplicateWithContentTask.status.isError) {
      this._showToast({
        type: 'ERROR',
        message: "Couldn't duplicate field with content",
        biName: 'cairo-field-action-duplicate-with-content-error',
        action: {
          text: 'Try again',
          onClick: () => this.duplicateWithContent(fieldId),
          removeToastOnClick: true
        }
      });
    }
  }
}
exports.FieldActionsState = FieldActionsState;
//# sourceMappingURL=FieldActionsState.js.map