/**
 * Rule: lifecycle-state-visible-in-detail
 *
 * For every model with a lifecycle, after creating a fresh entity
 * the UI should surface its current lifecycle state. v2 seeds an
 * entity in the initial state via the same pattern as the
 * state-sync rules (buildSeedData + createEntity) because an empty
 * detail view has nothing to assert against — a fresh instance is
 * the minimal setup that produces state-bearing UI.
 *
 * Lifecycle shape in the spec:
 *   lifecycles:
 *     status:
 *       flow: draft -> open -> closed
 *
 * We parse the `flow` string to extract states. If the model uses a
 * different shape (e.g. an explicit `states: [...]`), the parser
 * should normalize both into the same structure.
 *
 * One TestCase per (model, lifecycle) pair. Transitions between
 * states are tested by evolve-reflects-in-list; this rule is the
 * thin "initial state renders at all" counterpart.
 */
import { pickDisplayAttribute, buildSeedData } from './_shared.js';
export const RULE_ID = 'lifecycle-state-visible-in-detail';
function extractInitialState(lifecycleDef) {
    if (!lifecycleDef)
        return null;
    // Form 1: { flow: 'draft -> open -> closed' }
    if (typeof lifecycleDef.flow === 'string') {
        const first = lifecycleDef.flow.split('->')[0]?.trim();
        if (first)
            return first;
    }
    // Form 2: { states: ['draft', 'open', 'closed'] }
    if (Array.isArray(lifecycleDef.states) && lifecycleDef.states.length > 0) {
        const first = lifecycleDef.states[0];
        return typeof first === 'string' ? first : first?.name || null;
    }
    // Form 3: { initial: 'draft', states: {...} }
    if (typeof lifecycleDef.initial === 'string')
        return lifecycleDef.initial;
    return null;
}
export const lifecycleStateVisibleInDetail = (spec) => {
    const cases = [];
    for (const model of spec.models) {
        const lifecycles = model.lifecycles;
        if (!lifecycles)
            continue;
        // Need a way to seed an entity so there's something for the UI
        // to render. Skip the test for models without a seedable shape.
        const display = pickDisplayAttribute(model);
        if (!display)
            continue;
        const marker = `ct-lcstate-${model.name}`;
        const seedData = buildSeedData(model, marker);
        if (!seedData)
            continue;
        for (const [lcName, lcDef] of Object.entries(lifecycles)) {
            const initial = extractInitialState(lcDef);
            if (!initial)
                continue;
            cases.push({
                ruleId: RULE_ID,
                specElement: `${model.name}.${lcName}`,
                name: `[${RULE_ID}] ${model.name}.${lcName} initial state '${initial}' visible after create`,
                steps: [
                    { action: 'bootRuntime' },
                    { action: 'navigateToModel', modelName: model.name },
                    {
                        action: 'createEntity',
                        modelName: model.name,
                        data: seedData,
                        uniqueField: display.name,
                    },
                    { action: 'expectLifecycleState', modelName: model.name, stateName: initial },
                ],
            });
        }
    }
    return cases;
};
//# sourceMappingURL=lifecycle-state-visible-in-detail.js.map