2020-08-14 03:36:30 +00:00
|
|
|
import { FlagLabel, StateLabel, valueName } from './labels';
|
2020-08-12 00:14:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How a label changed.
|
|
|
|
*/
|
|
|
|
export enum ChangeEffect {
|
|
|
|
EXISTING = 'existing',
|
|
|
|
CREATED = 'created',
|
|
|
|
REMOVED = 'removed',
|
2020-08-12 03:19:01 +00:00
|
|
|
REQUIRED = 'required',
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Details of a label change.
|
|
|
|
*/
|
|
|
|
export interface ChangeRecord {
|
|
|
|
cause: string;
|
|
|
|
effect: ChangeEffect;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected inputs for a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveInput {
|
2020-08-12 01:39:53 +00:00
|
|
|
flags: Array<FlagLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array<string>;
|
2020-08-12 01:39:53 +00:00
|
|
|
states: Array<StateLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected results from a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveResult {
|
|
|
|
changes: Array<ChangeRecord>;
|
|
|
|
errors: Array<unknown>;
|
|
|
|
labels: Array<string>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveLabels(options: ResolveInput): ResolveResult {
|
|
|
|
const activeLabels = new Set(options.labels);
|
2020-08-12 03:19:01 +00:00
|
|
|
const changes: Array<ChangeRecord> = [];
|
|
|
|
const errors: Array<unknown> = [];
|
2020-08-12 00:14:42 +00:00
|
|
|
|
2020-08-12 01:39:53 +00:00
|
|
|
const sortedFlags = options.flags.sort((a, b) => a.priority - b.priority);
|
2020-08-12 00:14:42 +00:00
|
|
|
for (const flag of sortedFlags) {
|
|
|
|
const { name } = flag;
|
|
|
|
if (activeLabels.has(name)) {
|
|
|
|
// TODO: check removes
|
|
|
|
// TODO: check requires
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 01:39:53 +00:00
|
|
|
const sortedStates = options.states.sort((a, b) => a.priority - b.priority);
|
2020-08-12 00:14:42 +00:00
|
|
|
for (const state of sortedStates) {
|
|
|
|
for (const value of state.values) {
|
2020-08-14 03:36:30 +00:00
|
|
|
const name = valueName(state, value);
|
2020-08-12 00:14:42 +00:00
|
|
|
if (activeLabels.has(name)) {
|
|
|
|
// TODO: check removes
|
|
|
|
// TODO: check requires
|
|
|
|
// TODO: check becomes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-08-12 03:19:01 +00:00
|
|
|
changes,
|
|
|
|
errors,
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array.from(activeLabels),
|
|
|
|
};
|
|
|
|
}
|