"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withBackgrounds = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const preview_api_1 = require("storybook/internal/preview-api");
const manager_api_1 = require("storybook/manager-api");
const constants_1 = __importDefault(require("./constants"));
const container_1 = __importDefault(require("./container"));
exports.withBackgrounds = (0, preview_api_1.makeDecorator)({
    name: 'withBackgrounds',
    parameterName: 'backgrounds',
    skipIfNoParametersOrOptions: true,
    wrapper: (getStory, context, { options, parameters }) => {
        const data = (parameters || options || { values: [] });
        const backgrounds = data.values;
        let background = 'transparent';
        if (backgrounds.length !== 0) {
            manager_api_1.addons.getChannel().emit(constants_1.default.SET, backgrounds);
            const defaultValue = data.default
                ? backgrounds.find((b) => b.name === data.default)
                : undefined;
            const defaultOrFirst = defaultValue ? defaultValue : backgrounds[0];
            if (defaultOrFirst) {
                background = defaultOrFirst.value;
            }
        }
        return ((0, jsx_runtime_1.jsx)(container_1.default, { initialBackground: background, channel: manager_api_1.addons.getChannel(), children: getStory(context) }));
    },
});
