import { STOKEY_RULES, DEFAULT_RULES, KV_RULES_KEY } from "../config";
import { useStorage } from "./Storage";
import { checkRules } from "../libs/rules";
import { useCallback } from "react";

/**
 * 规则 hook
 * @returns
 */
export function useRules() {
  const { data: list = [], save } = useStorage(
    STOKEY_RULES,
    DEFAULT_RULES,
    KV_RULES_KEY
  );

  const add = useCallback(
    (rule) => {
      save((prev) => {
        if (
          rule.pattern === "*" ||
          prev.some((item) => item.pattern === rule.pattern)
        ) {
          return prev;
        }
        return [rule, ...prev];
      });
    },
    [save]
  );

  const del = useCallback(
    (pattern) => {
      save((prev) => {
        if (pattern === "*") {
          return prev;
        }
        return prev.filter((item) => item.pattern !== pattern);
      });
    },
    [save]
  );

  const clear = useCallback(() => {
    save((prev) => prev.filter((item) => item.pattern === "*"));
  }, [save]);

  const put = useCallback(
    (pattern, obj) => {
      save((prev) => {
        if (
          prev.some(
            (item) => item.pattern === obj.pattern && item.pattern !== pattern
          )
        ) {
          return prev;
        }
        return prev.map((item) =>
          item.pattern === pattern ? { ...item, ...obj } : item
        );
      });
    },
    [save]
  );

  const merge = useCallback(
    (rules) => {
      save((prev) => {
        const adds = checkRules(rules);
        if (adds.length === 0) {
          return prev;
        }

        const map = new Map();
        // 不进行深度合并
        // [...prev, ...adds].forEach((item) => {
        //   const k = item.pattern;
        //   map.set(k, { ...(map.get(k) || {}), ...item });
        // });
        prev.forEach((item) => map.set(item.pattern, item));
        adds.forEach((item) => map.set(item.pattern, item));
        return [...map.values()];
      });
    },
    [save]
  );

  return { list, add, del, clear, put, merge };
}
