const deleteRule = async (rule, allRulesList, fetchFromApi, entityDataForTab) => {
    const { id } = rule;
    const matchedRecord = allRulesList.find((rec) => rec.id === id);
    console.log("Matched ID :", matchedRecord);
    let automationTypeId;
    let currentRules,
      setRulesFunc,
      groupSetterFunc,
      currentTabPage,
      tabKey;

    console.log("Delete Triggered :", rule.id);

    switch (activeTab) {
      case "ticketcreate":
        automationTypeId = ticketCreate;
        currentRules = ticketCreationRules;
        setRulesFunc = setTicketCreationRules;
        groupSetterFunc = setTicketCreationRuleGroups;
        currentTabPage = currentPage.ticketcreate;
        tabKey = "ticketcreate";
        break;
      case "ticketupdate":
        automationTypeId = ticketUpdate;
        currentRules = ticketUpdateRules;
        setRulesFunc = setTicketUpdateRules;
        groupSetterFunc = setTicketUpdateRuleGroups;
        currentTabPage = currentPage.ticketupdate;
        tabKey = "ticketupdate";
        break;
      case "hourly":
        automationTypeId = hourlyTriggers;
        currentRules = hourlyTriggerRules;
        setRulesFunc = setHourlyTriggerRules;
        groupSetterFunc = setHourlyRuleGroups;
        currentTabPage = currentPage.hourly;
        tabKey = "hourly";
        break;
      default:
        return;
    }

    if (activeTab === "ticketcreate") {
      const data = await Filterrecords(ticketCreate, newRulePloadDB6.current);
      console.log("Delete-Ticket :", data)
      
    } else if (activeTab === "ticketupdate") {
      const data = await Filterrecords(ticketUpdate, newRulePloadDB6.current);
      console.log("Delete-Update :", data)
      
    } else if (activeTab === "hourly") {
      const data = await Filterrecords(hourlyTriggers, newRulePloadDB6.current);
      console.log("Delete-Hourly :", data)
      
    }



    const targetDisplayId = rule.display_id;
    if (targetDisplayId && newRulePloadDB6.current) {
      try {
        await newRulePloadDB6.current.delete(targetDisplayId);
        console.log("Deleted From Entity");
      } catch (err) { }
    }

    if (fetchFromApi) {
      console.log(`Deleting from Recent Data mode. Entity data for tab "${activeTab}":`, entityDataForTab);
    }

    try {
      const response = await client.request.invokeTemplate("deleterule", {
        context: {
          automationid: automationTypeId,
          ruleid: id,
        },
      });
      if (response.status === 204) {
        console.log("Deleted From api too.")
        // Remove deleted rule from the current flat rules list
        const updatedRules = currentRules.filter((r) => r.id !== id);
        setRulesFunc(updatedRules);
        if (ruleViewFilter.value === "duplicate_rules") {
          // Recalculate duplicate groups with updated rules after deletion
          const updatedGroups = groupDuplicates(updatedRules)
            .filter((g) => g.length > 1)
            // Optional: sort largest groups first or as you prefer
            .sort((a, b) => b.length - a.length);
          groupSetterFunc(updatedGroups);
          // Flatten the updated groups to find rules on current page
          const flattenUpdatedGroups = flattenGroups(updatedGroups);
          const filteredRules = getFilteredRules(flattenUpdatedGroups);
          const paginated = pagination(filteredRules, currentTabPage);
          // If current page empty and not first page, shift current page back
          if (paginated.length === 0 && currentTabPage > 1) {
            setCurrentPage((prev) => ({ ...prev, [tabKey]: Math.max(1, prev[tabKey] - 1) }));
          }
        } else {
          // In "all_rules" mode or others, just clear groups cache (optional)
          groupSetterFunc([]);
          const filteredRules = getFilteredRules(updatedRules);
          const paginated = pagination(filteredRules, currentTabPage);
          if (paginated.length === 0 && currentTabPage > 1) {
            setCurrentPage((prev) => ({ ...prev, [tabKey]: Math.max(1, prev[tabKey] - 1) }));
          }
        }
        toast.current.trigger({ type: "success", content: "Rule deleted successfully!" });
      } else {
        toast.current.trigger({ type: "error", content: "Failed to delete rule." });
      }


    } catch (error) {
      console.log(error);
      toast.current.trigger({ type: "error", content: "Error deleting rule." });
    }

  };


now in this, rule.id will have a id, if fetchfromapi, which is active, match with id to find which rule in entity is going to be deleted