/**
 * Copyright (c) 2023-present Pi Dash Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import type { TWorkItemFilterCondition } from "@pi-dash/shared-state";
import type { TFilterConditionNodeForDisplay, TFilterValue, TWorkItemFilterProperty } from "@pi-dash/types";

export const PROGRESS_STATS = [
  {
    key: "stat-states",
    i18n_title: "States",
  },
  {
    key: "stat-assignees",
    i18n_title: "Assignees",
  },
  {
    key: "stat-labels",
    i18n_title: "Labels",
  },
];

type TSelectedFilterProgressStatsType = TFilterConditionNodeForDisplay<TWorkItemFilterProperty, TFilterValue>;

export type TSelectedFilterProgressStats = {
  assignees: TSelectedFilterProgressStatsType | undefined;
  labels: TSelectedFilterProgressStatsType | undefined;
  stateGroups: TSelectedFilterProgressStatsType | undefined;
};

export const createFilterUpdateHandler =
  <T extends string>(
    property: TWorkItemFilterProperty,
    selectedValues: T[],
    handleFiltersUpdate: (condition: TWorkItemFilterCondition) => void
  ) =>
  (value: T | undefined) => {
    const updatedValues = value ? [...selectedValues] : [];

    if (value) {
      if (updatedValues.includes(value)) {
        updatedValues.splice(updatedValues.indexOf(value), 1);
      } else {
        updatedValues.push(value);
      }
    }

    handleFiltersUpdate({ property, operator: "in", value: updatedValues });
  };
