"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetThemeColorUC = void 0;
exports.makeSetThemeColorUC = makeSetThemeColorUC;
const core_1 = require("@vived/core");
const ThemeColors_1 = require("../../ThemeColors");
const Entities_1 = require("../Entities");
class SetThemeColorUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_THEME_COLORS";
        this.payloadVersion = 1;
    }
}
exports.SetThemeColorUC = SetThemeColorUC;
SetThemeColorUC.type = "SetThemeColorsBaseUC";
function makeSetThemeColorUC(appObject) {
    return new SetThemeColorUCImp(appObject);
}
class SetThemeColorUCImp extends SetThemeColorUC {
    castPayloadV1(payload) {
        const castPayload = payload;
        if (castPayload.colors === undefined) {
            throw new Entities_1.UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, SetThemeColorUC.type);
        this.action = (payload) => {
            const { colors } = payload;
            const themeColors = ThemeColors_1.ThemeColors.get(this.appObject.appObjectRepo);
            if (!themeColors) {
                return;
            }
            Object.keys(colors).forEach((schemeType) => {
                const type = schemeType;
                const hex = colors[schemeType];
                themeColors.setColorHexForType(type, hex);
            });
        };
        this.handleRequest = (version, payload) => {
            if (version === 1) {
                const castPayload = this.castPayloadV1(payload);
                this.action(castPayload);
            }
            else {
                throw new Entities_1.UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(Entities_1.AppHandlerEntity.type);
        if (!handler) {
            this.warn("SetThemeColorsBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=SetThemeColorUC.js.map