export class DispenserSelector {
  constructor(handlers) {
    this.handlers = handlers;
  }

  async handleAction(action, event, actionEl) {
    const recipeId = actionEl.dataset.recipeId;
    const periodId = actionEl.dataset.periodId;
    const dispenserId = actionEl.dataset.dispenserId;
    const index = Number(actionEl.dataset.index);

    switch (action) {
      case "select-dispenser":
        if (dispenserId) await this.handlers.selectDispenser(dispenserId);
        return true;
      case "select-recipe":
        if (recipeId) await this.handlers.selectRecipe(recipeId);
        return true;
      case "select-period":
        if (periodId) await this.handlers.selectPeriod(periodId);
        return true;
      case "copy-recipe":
        event.stopPropagation();
        if (recipeId) await this.handlers.copyRecipe(recipeId);
        return true;
      case "delete-recipe":
        event.stopPropagation();
        if (recipeId) await this.handlers.deleteRecipe(recipeId);
        return true;
      case "paste-recipe":
        event.stopPropagation();
        if (periodId) await this.handlers.pasteRecipe(periodId);
        return true;
      case "create-recipe":
        event.stopPropagation();
        if (periodId) await this.handlers.createNewRecipe(periodId);
        return true;
      case "move-recipe-up":
        event.stopPropagation();
        if (recipeId && !Number.isNaN(index)) {
          await this.handlers.moveRecipeUp(recipeId, index);
        }
        return true;
      case "move-recipe-down":
        event.stopPropagation();
        if (recipeId && !Number.isNaN(index)) {
          await this.handlers.moveRecipeDown(recipeId, index);
        }
        return true;
      case "unskip-recipe-today":
        event.stopPropagation();
        event.preventDefault();
        if (recipeId && typeof this.handlers.unskipRecipeToday === "function") {
          await this.handlers.unskipRecipeToday(recipeId);
        }
        return true;
      case "unskip-ingredient-parts-today":
        event.stopPropagation();
        event.preventDefault();
        if (recipeId && typeof this.handlers.unskipIngredientPartsToday === "function") {
          await this.handlers.unskipIngredientPartsToday(recipeId);
        }
        return true;
      case "unskip-group-parts-today":
        event.stopPropagation();
        event.preventDefault();
        if (recipeId && typeof this.handlers.unskipGroupPartsToday === "function") {
          await this.handlers.unskipGroupPartsToday(recipeId);
        }
        return true;
      case "recipe-drag-handle":
        event.stopPropagation();
        return true;
      default:
        return false;
    }
  }
}
