import { createSelector } from '@reduxjs/toolkit';

import { buildGroupFilter } from '../../../common/InventoryFilter';
import type { GroupDetailState } from '.';

interface WithGroupDetail {
  groupDetail: GroupDetailState;
}

const selectFilterState = (state: WithGroupDetail) => state.groupDetail.filterState;
const selectOriginalFilterState = (state: WithGroupDetail) => state.groupDetail.originalFilterState;

// Dirty when the savable payload differs from the baseline; comparing payloads
// (not raw rows) ignores incomplete rules, matching what a save persists.
export const selectIsFilterDirty = createSelector(selectFilterState, selectOriginalFilterState, (current, original) => JSON.stringify(buildGroupFilter(current)) !== JSON.stringify(buildGroupFilter(original)));
