"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const errors = require("../../../../base/common/errors.js");
const lazy = require("../../../../base/common/lazy.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const messageController = require("../../message/browser/messageController.js");
const instantiation = require("../../../../platform/instantiation/common/instantiation.js");
const codeActionMenu = require("./codeActionMenu.js");
const lightBulbWidget = require("./lightBulbWidget.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
var __classPrivateFieldSet = globalThis && globalThis.__classPrivateFieldSet || function(receiver, state, value, kind, f) {
  if (kind === "m")
    throw new TypeError("Private method is not writable");
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a setter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
};
var __classPrivateFieldGet = globalThis && globalThis.__classPrivateFieldGet || function(receiver, state, kind, f) {
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a getter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _CodeActionUi_disposed;
exports.CodeActionUi = class CodeActionUi extends lifecycle.Disposable {
  constructor(_editor, quickFixActionId, preferredFixActionId, delegate, instantiationService) {
    super();
    this._editor = _editor;
    this.delegate = delegate;
    this._activeCodeActions = this._register(new lifecycle.MutableDisposable());
    _CodeActionUi_disposed.set(this, false);
    this._codeActionWidget = new lazy.Lazy(() => {
      return this._register(instantiationService.createInstance(codeActionMenu.CodeActionMenu, this._editor, {
        onSelectCodeAction: (action) => __awaiter(this, void 0, void 0, function* () {
          this.delegate.applyCodeAction(
            action,
            /* retrigger */
            true
          );
        })
      }));
    });
    this._lightBulbWidget = new lazy.Lazy(() => {
      const widget = this._register(instantiationService.createInstance(lightBulbWidget.LightBulbWidget, this._editor, quickFixActionId, preferredFixActionId));
      this._register(widget.onClick((e) => this.showCodeActionList(e.trigger, e.actions, e, { includeDisabledActions: false })));
      return widget;
    });
  }
  dispose() {
    __classPrivateFieldSet(this, _CodeActionUi_disposed, true, "f");
    super.dispose();
  }
  update(newState) {
    var _a, _b, _c, _d, _e;
    return __awaiter(this, void 0, void 0, function* () {
      if (newState.type !== 1) {
        (_a = this._lightBulbWidget.rawValue) === null || _a === void 0 ? void 0 : _a.hide();
        return;
      }
      let actions;
      try {
        actions = yield newState.actions;
      } catch (e) {
        errors.onUnexpectedError(e);
        return;
      }
      if (__classPrivateFieldGet(this, _CodeActionUi_disposed, "f")) {
        return;
      }
      this._lightBulbWidget.getValue().update(actions, newState.trigger, newState.position);
      if (newState.trigger.type === 1) {
        if ((_b = newState.trigger.filter) === null || _b === void 0 ? void 0 : _b.include) {
          const validActionToApply = this.tryGetValidActionToApply(newState.trigger, actions);
          if (validActionToApply) {
            try {
              this._lightBulbWidget.getValue().hide();
              yield this.delegate.applyCodeAction(validActionToApply, false);
            } finally {
              actions.dispose();
            }
            return;
          }
          if (newState.trigger.context) {
            const invalidAction = this.getInvalidActionThatWouldHaveBeenApplied(newState.trigger, actions);
            if (invalidAction && invalidAction.action.disabled) {
              (_c = messageController.MessageController.get(this._editor)) === null || _c === void 0 ? void 0 : _c.showMessage(invalidAction.action.disabled, newState.trigger.context.position);
              actions.dispose();
              return;
            }
          }
        }
        const includeDisabledActions = !!((_d = newState.trigger.filter) === null || _d === void 0 ? void 0 : _d.include);
        if (newState.trigger.context) {
          if (!actions.allActions.length || !includeDisabledActions && !actions.validActions.length) {
            (_e = messageController.MessageController.get(this._editor)) === null || _e === void 0 ? void 0 : _e.showMessage(newState.trigger.context.notAvailableMessage, newState.trigger.context.position);
            this._activeCodeActions.value = actions;
            actions.dispose();
            return;
          }
        }
        this._activeCodeActions.value = actions;
        this._codeActionWidget.getValue().show(newState.trigger, actions, newState.position, { includeDisabledActions });
      } else {
        if (this._codeActionWidget.getValue().isVisible) {
          actions.dispose();
        } else {
          this._activeCodeActions.value = actions;
        }
      }
    });
  }
  getInvalidActionThatWouldHaveBeenApplied(trigger, actions) {
    if (!actions.allActions.length) {
      return void 0;
    }
    if (trigger.autoApply === "first" && actions.validActions.length === 0 || trigger.autoApply === "ifSingle" && actions.allActions.length === 1) {
      return actions.allActions.find(({ action }) => action.disabled);
    }
    return void 0;
  }
  tryGetValidActionToApply(trigger, actions) {
    if (!actions.validActions.length) {
      return void 0;
    }
    if (trigger.autoApply === "first" && actions.validActions.length > 0 || trigger.autoApply === "ifSingle" && actions.validActions.length === 1) {
      return actions.validActions[0];
    }
    return void 0;
  }
  showCodeActionList(trigger, actions, at, options) {
    return __awaiter(this, void 0, void 0, function* () {
      this._codeActionWidget.getValue().show(trigger, actions, at, options);
    });
  }
};
_CodeActionUi_disposed = /* @__PURE__ */ new WeakMap();
exports.CodeActionUi = __decorate([
  __param(4, instantiation.IInstantiationService)
], exports.CodeActionUi);
