"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnableFeatureUC = void 0;
exports.makeEnableFeatureUC = makeEnableFeatureUC;
const core_1 = require("@vived/core");
const FeatureFlags_1 = require("../../FeatureFlags");
const Entities_1 = require("../Entities");
class EnableFeatureUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "ENABLE_FEATURE";
        this.payloadVersion = 1;
    }
}
exports.EnableFeatureUC = EnableFeatureUC;
EnableFeatureUC.type = "EnableFeatureUC";
function makeEnableFeatureUC(appObject) {
    return new EnableFeatureUCImp(appObject);
}
class EnableFeatureUCImp extends EnableFeatureUC {
    get featureFlagEntity() {
        return this.getCachedSingleton(FeatureFlags_1.FeatureFlagsEntity.type);
    }
    castPayload(payload) {
        const castPayload = payload;
        if (castPayload.featureFlag === undefined) {
            throw new Entities_1.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 Entities_1.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_1.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