import { DIVISION_TYPE, DIVISION_CATEGORIES } from "@/constants";
import { useMemo } from "react";

/**
 * Map division type and filters to division categories.
 *
 * Converts internal division type ('branch' or 'postomat') and filter settings
 * into divisionCategories array.
 *
 * @param {string} divisionType - Division type (DIVISION_TYPE.BRANCH or DIVISION_TYPE.POSTMAT)
 * @param {Object} divisionsFilters - Filter configuration
 * @param {boolean} [divisionsFilters.postBranch] - Enable PostBranch divisions
 * @param {boolean} [divisionsFilters.cargoBranch] - Enable CargoBranch divisions
 * @param {boolean} [divisionsFilters.postomat] - Enable Postomat divisions
 * @param {boolean} [divisionsFilters.pudo] - Enable PUDO divisions
 * @returns {string[]} Array of division categories
 */
export const useDivisionCategories = (divisionType, divisionsFilters = {}) => {
  const divisionCategories = useMemo(() => {
    if (divisionType === DIVISION_TYPE.POSTMAT) {
      return [DIVISION_CATEGORIES.POSTOMAT];
    }

    const {
      postBranch = false,
      cargoBranch = false,
      postomat = false,
      pudo = false,
    } = divisionsFilters;

    if (divisionType === DIVISION_TYPE.BRANCH) {
      const categories = [];

      const allFiltersDisabled = !postBranch && !cargoBranch && !pudo;

      if (allFiltersDisabled || postBranch) {
        categories.push(DIVISION_CATEGORIES.POST_BRANCH);
      }

      if (allFiltersDisabled || cargoBranch) {
        categories.push(DIVISION_CATEGORIES.CARGO_BRANCH);
      }

      if (allFiltersDisabled || pudo) {
        categories.push(DIVISION_CATEGORIES.PUDO);
      }

      return categories.length > 0
        ? categories
        : [
            DIVISION_CATEGORIES.POST_BRANCH,
            DIVISION_CATEGORIES.CARGO_BRANCH,
            DIVISION_CATEGORIES.PUDO,
          ];
    }

    return [
      DIVISION_CATEGORIES.POST_BRANCH,
      DIVISION_CATEGORIES.CARGO_BRANCH,
      DIVISION_CATEGORIES.POSTOMAT,
      DIVISION_CATEGORIES.PUDO,
    ];
  }, [divisionType, divisionsFilters]);

  return {
    divisionCategories,
  };
};
