import type { Services } from '../../../services/mod.js' import type { DeviceRegistrationManifest } from '../../../domain/device-registration-manifest.js' import { isError, makeError, makeSuccess, unwrapResult, } from '../../../framework/types/result.js' import { BaseError } from '../../../framework/error/mod.js' interface Options { deps: { services: Services } } export interface GetFeatureFlagDto { manifest: DeviceRegistrationManifest key: FlagKey } export class GetFeatureFlagError extends BaseError { public readonly _tag = 'GetFeatureFlagError' } export function buildGetFeatureFlag(options: Options) { return async function getFeatureFlag( dto: GetFeatureFlagDto, ) { const { deps: { services }, } = options const resultOfGettingLicense = await services.device.getLicense( dto.manifest, ) if (isError(resultOfGettingLicense)) { return makeError(new GetFeatureFlagError()) } const license = unwrapResult(resultOfGettingLicense) const flag = license.flags?.find((flag) => flag.key === dto.key) return makeSuccess(flag?.value) } }