/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BasePolicy } from './basePolicy.ts';
import type { CategoryDto, PolicyDto } from './policyDto.ts';
import { renderProfileString } from './render.ts';
import { type Category, type NlsString, PolicyType, type LanguageTranslations } from './types.ts';
export class StringEnumPolicy extends BasePolicy {
static from(category: CategoryDto, policy: PolicyDto): StringEnumPolicy | undefined {
const { type, name, minimumVersion, enum: enumValue, localization } = policy;
if (type !== 'string') {
return undefined;
}
const enum_ = enumValue;
if (!enum_) {
return undefined;
}
if (!localization.enumDescriptions || !Array.isArray(localization.enumDescriptions) || localization.enumDescriptions.length !== enum_.length) {
throw new Error(`Invalid policy data: enumDescriptions must exist and have the same length as enum_ for policy "${name}".`);
}
const enumDescriptions = localization.enumDescriptions.map((e) => ({ nlsKey: e.key, value: e.value }));
return new StringEnumPolicy(
name,
{ moduleName: '', name: { nlsKey: category.name.key, value: category.name.value } },
minimumVersion,
{ nlsKey: localization.description.key, value: localization.description.value },
'',
enum_,
enumDescriptions
);
}
protected enum_: string[];
protected enumDescriptions: NlsString[];
private constructor(
name: string,
category: Category,
minimumVersion: string,
description: NlsString,
moduleName: string,
enum_: string[],
enumDescriptions: NlsString[],
) {
super(PolicyType.StringEnum, name, category, minimumVersion, description, moduleName);
this.enum_ = enum_;
this.enumDescriptions = enumDescriptions;
}
protected renderADMXElements(): string[] {
return [
``,
...this.enum_.map((value, index) => ` - ${value}
`),
``
];
}
renderADMLStrings(translations?: LanguageTranslations) {
return [
...super.renderADMLStrings(translations),
...this.enumDescriptions.map(e => this.renderADMLString(e, translations))
];
}
renderADMLPresentationContents() {
return ``;
}
renderJsonValue() {
return this.enum_[0];
}
renderProfileValue() {
return `${this.enum_[0]}`;
}
renderProfileManifestValue(translations?: LanguageTranslations): string {
return `pfm_default
${this.enum_[0]}
pfm_description
${renderProfileString(this.name, this.moduleName, this.description, translations)}
pfm_name
${this.name}
pfm_title
${this.name}
pfm_type
string
pfm_range_list
${this.enum_.map(e => `${e}`).join('\n ')}
`;
}
}