"""Removing a select / multiselect OPTION carries its stored VALUES out with it. Owner, 2026-09-02: *"Removing an Option from the Single-select / Multi-select if the option is already being used by a cell should give a warning before deleting. And after confirmation, it should allow the user to immediately delete the option and that option's values in all the cells in the field."* This module is the PURE half: it knows how a choice value is shaped in each stratum and how to strip one option out of it, and it counts. It reads no store and writes none. The strata each own their write (`table_store.TableStore.remove_choice_values` for a person's workspace, `shared_overlay.purge_choice_values` for the tenant-wide bucket, `user_tables.remove_choice_values` for a user database), and `routes_grid` composes them behind one door, so the client asks one question - *how many cells use it?* - and issues one confirmed write. Three cell shapes exist, and every one of them has shipped: * a SCALAR string (`select`, and a `multiselect` written by the connector as a comma-joined SET - `aios_grid_fields.json` documents that shape for customer tags and product supplier); * a LIST of member strings (a `multiselect` written by the grid's own overlay patch); * `None` / `''`, which is "no value" and never a member. Matching is case-insensitive, exactly as `aios_grid._clean_options` de-duplicates the vocabulary: a cell holding `acme` is a use of the option `Acme`, because the picker would show them as one. """ def normalise(options): """Trimmed, non-empty, case-insensitively de-duplicated option labels, order kept.""" out, seen = [], set() for raw in list(options or ()): if isinstance(raw, bool) or not isinstance(raw, (str, int, float)): continue label = str(raw).strip()[:120] if not label or label.casefold() in seen: continue seen.add(label.casefold()) out.append(label) return out def folded(options): """The case-insensitive match set for `options`.""" return {label.casefold() for label in normalise(options)} def _members(value, multi): """The individual labels a stored cell represents, or None when it holds no value.""" if value is None or isinstance(value, bool): return None if isinstance(value, (list, tuple, set)): parts = [str(v).strip() for v in value if str(v or '').strip()] return parts text = str(value).strip() if not text: return None if multi: return [p.strip() for p in text.split(',') if p.strip()] return [text] def uses(value, removed_fold, *, multi): """True when this cell holds at least one of the removed options.""" parts = _members(value, multi) if not parts: return False return any(p.casefold() in removed_fold for p in parts) def strip_value(value, removed_fold, *, multi, blank=''): """`(new_value, changed)` - the cell with every removed option taken out. The SHAPE is preserved: a list stays a list, a comma-joined string stays comma-joined, and a scalar becomes `blank`. `blank` is the stratum's own "no value" - `''` for a person's overlay and a user database, and whatever sentinel a seeded column uses (Product Supplier keeps `'(none)'`, because an EMPTY shared cell there means "fall back to the mastersheet default", which would put the removed option straight back on the row). """ parts = _members(value, multi) if not parts: return value, False kept = [p for p in parts if p.casefold() not in removed_fold] if len(kept) == len(parts): return value, False if isinstance(value, (list, tuple, set)): return (list(kept) if kept else (blank if blank not in ('', None) else [])), True if multi: return (', '.join(dict.fromkeys(kept)) if kept else blank), True return blank, True def count_cells(rows, key, removed_fold, *, multi): """How many of `rows` (an iterable of `{field_key: value}` dicts) use a removed option.""" n = 0 for row in rows or (): if isinstance(row, dict) and key in row and uses(row.get(key), removed_fold, multi=multi): n += 1 return n def without(options, removed_fold): """The vocabulary with the removed labels taken out. Accepts the two stored shapes - plain strings, and legacy `{'name': ...}` choice objects - and keeps whichever it got.""" out = [] for choice in list(options or ()): if isinstance(choice, dict): label = str(choice.get('name') or '').strip() else: label = str(choice or '').strip() if label and label.casefold() in removed_fold: continue out.append(choice) return out