import { AppObjectUC } from "@vived/core";
import { FeatureFlagsEntity } from "../../FeatureFlags";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class EnableFeatureUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "ENABLE_FEATURE";
        this.payloadVersion = 1;
    }
}
EnableFeatureUC.type = "EnableFeatureUC";
export function makeEnableFeatureUC(appObject) {
    return new EnableFeatureUCImp(appObject);
}
class EnableFeatureUCImp extends EnableFeatureUC {
    get featureFlagEntity() {
        return this.getCachedSingleton(FeatureFlagsEntity.type);
    }
    castPayload(payload) {
        const castPayload = payload;
        if (castPayload.featureFlag === undefined) {
            throw new UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, EnableFeatureUC.type);
        this.action = (featureFlag) => {
            this.featureFlagEntity?.addFlag(featureFlag);
        };
        this.handleRequest = (version, payload) => {
            if (version === this.payloadVersion) {
                const { featureFlag } = this.castPayload(payload);
                this.action(featureFlag);
            }
            else {
                throw new UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(AppHandlerEntity.type);
        if (!handler) {
            this.warn("DisposeAppBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=EnableFeatureUC.js.map