"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Color = exports.colorType = exports.colorMarkName = exports.Colors = exports.colorClassName = void 0;
const prosemirror_commands_1 = require("prosemirror-commands");
const marks_1 = require("../../../utils/marks.js");
const ColorSpecs_1 = require("./ColorSpecs/index.js");
const const_1 = require("./const.js");
const utils_1 = require("./utils.js");
require("./colors.css");
var const_2 = require("./const.js");
Object.defineProperty(exports, "colorClassName", { enumerable: true, get: function () { return const_2.colorClassName; } });
Object.defineProperty(exports, "Colors", { enumerable: true, get: function () { return const_2.Colors; } });
var ColorSpecs_2 = require("./ColorSpecs/index.js");
Object.defineProperty(exports, "colorMarkName", { enumerable: true, get: function () { return ColorSpecs_2.colorMarkName; } });
Object.defineProperty(exports, "colorType", { enumerable: true, get: function () { return ColorSpecs_2.colorType; } });
const Color = (builder) => {
    builder.use(ColorSpecs_1.ColorSpecs, {
        validateClassNameColorName: utils_1.validateClassNameColorName,
        parseStyleColorValue: utils_1.parseStyleColorValue,
    });
    builder.addAction(const_1.colorAction, ({ schema }) => {
        const type = (0, ColorSpecs_1.colorType)(schema);
        return {
            isActive: (state) => Boolean((0, marks_1.isMarkActive)(state, type)),
            isEnable: (0, prosemirror_commands_1.toggleMark)(type),
            run: (state, dispatch, _view, attrs) => {
                const params = attrs;
                const hasMark = (0, marks_1.isMarkActive)(state, type);
                if (!params || !params[const_1.colorMarkName]) {
                    if (!hasMark)
                        return true;
                    // remove mark
                    return (0, prosemirror_commands_1.toggleMark)(type, params)(state, dispatch);
                }
                if (hasMark) {
                    // remove old mark, then add new with new color
                    return (0, utils_1.chainAND)((0, prosemirror_commands_1.toggleMark)(type), (0, prosemirror_commands_1.toggleMark)(type, params))(state, dispatch);
                }
                // add mark
                return (0, prosemirror_commands_1.toggleMark)(type, params)(state, dispatch);
            },
            meta(state) {
                return type.isInSet(state.selection.$to.marks())?.attrs[const_1.colorMarkName];
            },
        };
    });
};
exports.Color = Color;
//# sourceMappingURL=index.js.map
