/**
 * Pivot aggregate detection and frontend file exclusion helpers.
 *
 * In Aurora, a pivot aggregate is auto-derived from a many-to-many relationship
 * declared in a parent aggregate's YAML. Pivots are NEVER first-class frontend
 * subjects — they exist as backend storage and as a typed surface for the
 * manager widget that represents the m2m on a parent's form. This module
 * encapsulates the structural detection rule and the list of frontend UI
 * files that must be skipped during pivot emission.
 *
 * See `openspec/changes/archive/2026-05-06-skip-pivot-frontend-emission/`
 * for the architectural rationale.
 */
import { DELETED_AT_FIELD, TIMESTAMP_FIELDS } from './config.js';
const SYSTEM_PROPERTY_NAMES = new Set([
    'id',
    'rowId',
    ...TIMESTAMP_FIELDS,
    ...DELETED_AT_FIELD,
]);
/**
 * Returns true iff the schema represents a pivot aggregate auto-derived from
 * a many-to-many relationship.
 *
 * Detection rule (structural): every non-system property of the schema must
 * be a `relationship.type === 'many-to-one'`. System properties (`id`,
 * `rowId`, timestamps, `deletedAt`) are excluded from the check.
 *
 * A schema with no FKs at all (only system properties) is NOT a pivot — a
 * pivot must have at least one many-to-one FK pointing to one of the m2m
 * parents.
 */
export const isPivotAggregate = (schema) => {
    const props = schema.aggregateProperties ?? [];
    if (props.length === 0)
        return false;
    const nonSystemProps = props.filter((p) => !SYSTEM_PROPERTY_NAMES.has(p.name));
    if (nonSystemProps.length === 0)
        return false;
    return nonSystemProps.every((p) => p.relationship?.type === 'many-to-one');
};
/**
 * Returns the list of frontend UI file paths that must be excluded when
 * emitting a pivot aggregate's module. These files are unsuitable for pivots
 * because pivot types have composite primary keys (no single `id`) and no
 * `name` field, so the templates' references to `<row>.id` and `<row>?.name`
 * fail TS2339 at compile time.
 *
 * The matching is performed by `isFilePathAllowed` via `endsWith` or exact
 * match; plain filenames (no path prefix) are sufficient.
 *
 * @param modKebab the kebab-case module name of the pivot (e.g. `permission-role`)
 */
export const getPivotFrontendExcludedFiles = (modKebab) => [
    `${modKebab}-list.component.ts`,
    `${modKebab}-detail.component.ts`,
    `${modKebab}-form.component.ts`,
    `${modKebab}-form-embed.component.ts`,
    `${modKebab}-list-actions-cell.component.ts`,
    `${modKebab}.columns.ts`,
    // Pivot's barrel re-exports `<mod>.columns`. With columns excluded, the
    // barrel would dangle (TS2305). Skip it entirely; nothing imports the
    // pivot's barrel (verified by grep).
    `index.ts`,
    // Pivot resolvers are dead code: pivots have no standalone routes (no list /
    // detail components that consume a route resolver). The emitted resolvers
    // file imports `<mod>.columns` which is excluded above, so emitting it
    // would fail TS2307 ("Cannot find module"). Skip.
    `${modKebab}.resolvers.ts`,
];
/**
 * Returns the list of pivot frontend files that the codegen DOES emit (used
 * by the orphan-warning emitter to label them as "KEEP" alongside the
 * "delete" list).
 *
 * @param modKebab the kebab-case module name of the pivot
 */
export const getPivotFrontendKeptFiles = (modKebab) => [
    `data-access/${modKebab}.list-config.ts`,
    `data-access/${modKebab}.detail-config.ts`,
    `data-access/${modKebab}.graphql.ts`,
];
