'use strict';

var palette = require('../../../design/palette.js');
var Doc = require('../../util/Doc.js');

const Chip = ({ text, x, y, transformOrigin = 'center', size = 'm', color = palette.default.violet500, variant = 'filled', }) => async (doc) => {
    const paddingX = 8;
    const paddingY = size === 'm' ? 2 : size === 'l' ? 4 : 0;
    const textWidth = doc.fontSize(8).widthOfString(text, { align: 'center' });
    const textHeight = doc.heightOfString(text, { align: 'center' });
    const width = textWidth + 2 * paddingX;
    const height = textHeight + 2 * paddingY;
    const offsetX = transformOrigin === 'left'
        ? x
        : transformOrigin === 'center'
            ? x - width / 2
            : transformOrigin === 'right'
                ? x - width
                : 0;
    const offsetY = y;
    // border
    if (variant === 'filled') {
        doc.roundedRect(offsetX, offsetY, width, height, 999).fillColor(color, 0.1).fill();
    }
    else if (variant === 'outlined') {
        doc
            .roundedRect(offsetX, offsetY, width, height, 999)
            .lineWidth(0.5)
            .strokeColor(color)
            .stroke();
    }
    const textOffsetX = transformOrigin === 'left'
        ? x + paddingX
        : transformOrigin === 'center'
            ? x - textWidth / 2
            : transformOrigin === 'right'
                ? x - textWidth - paddingX
                : 0;
    const textOffsetY = y + paddingY;
    // text
    Doc.Doc.font('normal');
    doc.fillColor(color, 1).text(text, textOffsetX, textOffsetY);
};

exports.Chip = Chip;
