# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

"Rozliczenia Maszynka" is a Chrome Extension (Manifest V3) that automates settlement calculations in CRM Społeczności. It injects into invoice edit pages at `https://crm.spolecznosci.pl/settlements/invoice/edit/*` and provides UI controls for batch price modifications using CSV-based rules.

## Build Command

```bash
node build.js
```

This concatenates all source files from `src/` into a single IIFE in `dist/content.js`. The build order matters - files are concatenated in the order specified in `build.js`.

## Architecture

The extension uses a class-based architecture without external dependencies:

### Core Classes (in dependency order)

1. **constants.js** - `CONSTANTS` object with selectors, column names, design tokens, keyboard shortcuts
2. **utils.js** - Utility classes:
   - `CSVUtils` - CSV parsing with quote handling
   - `DOMHelpers` - DOM element creation with Design System styling
   - `ValueExtractor` - Extract values from CRM's DOM structure
3. **models.js** - Data models:
   - `RowData` - Represents a table row with all its values (RC, K, S, quantity, views)
   - `PatternMatcher` - Glob-like pattern matching for rules
   - `ExpressionEvaluator` - Evaluates expressions like `[RC]*0.6` using row data
4. **services.js** - External integrations:
   - `CRMService` - Wrapper for CRM's global functions (`recalcRow`, `saveManyRow`, `saveRow`)
   - `PriceListManager` - Fetches and caches price list CSV from CRM
   - `CSVHandler` - CSV import/export with header management
5. **rules.js** - Rule processing:
   - `RuleParser` - Parses CSV rule format: `wydawca,serwis,format,wartość`
   - `RuleApplier` - Applies rules to matching rows, handles batch saves
6. **ui.js** - UI components:
   - `UIManager` - Creates control panel, inputs, buttons, actions/filters sections
   - `RowVisibilityManager` - Toggle visibility of zero-quantity rows
7. **events.js** - `EventManager` - Keyboard shortcuts, cell clicks, warning icons, MutationObserver for dynamic content
8. **campaign-table.js** - `CampaignTable` - Main orchestrator class that wires everything together
9. **main.js** - Entry point, waits for DOM and initializes `CampaignTable` instances

### Key Concepts

**Rules Format (AKCJE)**: CSV with 4 columns: `wydawca,serwis,format,wartość`
- Wildcards: `*` matches anything
- Expressions: `[RC]*0.6`, `[K]-0.5`, `[S]+1` (variables: RC, K, S)
- Applied via RÓB (single) or RÓB WSZYSTKIE (batch from textarea)

**Filters Format (FILTRY)**: CSV with 3 columns: `wydawca,serwis,format`
- Used to highlight matching rows for review
- Applied via FILTR (single, adds to textarea) or FILTR WSZYSTKIE (batch)
- Include/Exclude toggle determines highlight color (green/red)

**Include/Exclude Toggle**:
- Two synchronized toggles: button I|E (top panel) and slider (bottom panel)
- Include mode: matching rows highlighted green
- Exclude mode: matching rows highlighted red
- `handleToggleChange()` in `CampaignTable` keeps both in sync

**UI Layout**:
- Top panel: UKRYJ | WYZERUJ | WYCZYŚĆ OZNACZENIA | WYCZYŚĆ FILTR | [inputs] | WYCZYŚĆ | RÓB | FILTR | [I|E] | [Ilość] | [Odsłony] | ZASTOSUJ
- Bottom section: Two flex-wrap boxes side by side:
  - AKCJE: textarea + RÓB WSZYSTKIE/WCZYTAJ CSV/ZAPISZ CSV
  - FILTRY: textarea + FILTR WSZYSTKIE + slider I|E + stats display

**CRM Integration**: The extension relies on CRM's global functions. Uses `"world": "MAIN"` in manifest.json to run in page context (not isolated content script).

**Keyboard Shortcuts** (when focused on unit cost input):
- Arrow Up/Down: Adjust value by 0.1
- X/Z: Navigate to next/previous row
- E: Toggle layout switcher
- M: Set minimum price from `d-min-price` attribute

## Important Implementation Details

- `"world": "MAIN"` in manifest.json is critical - allows access to page's global functions (`recalcRow`, `saveManyRow`, `saveRow`)
- Filter state stored in `CampaignTable`: `matchedFilterRowIds`, `currentSums`
- CSS classes for row highlighting: `filter-matched` (green), `filter-exclude` (red), `modified-by-plugin` (purple)
- Toggle sync: top toggle is source of truth (`toggleIE_top`), bottom slider has `updateVisual()` method

## Language

The codebase and UI are in Polish. Variable names and class names are in English.
