import colorize from './colorize.js';
import { toStyledCharacters } from './measure-text.js';
export const renderScrollbar = ({ layout, clip, axis, color, setChar, getExistingChar, }) => {
    const { x, y, thumb: { start: startIndex, end: endIndex, startHalf: thumbStartHalf, endHalf: thumbEndHalf, }, } = layout;
    if (axis === 'vertical') {
        for (let i = startIndex; i < endIndex; i++) {
            const drawY = y + i;
            const drawX = x;
            if (drawY >= clip.y &&
                drawY < clip.y + clip.h &&
                drawX >= clip.x &&
                drawX < clip.x + clip.w) {
                let char = '█';
                const hasUpper = 2 * i >= thumbStartHalf && 2 * i < thumbEndHalf;
                const hasLower = 2 * i + 1 >= thumbStartHalf && 2 * i + 1 < thumbEndHalf;
                if (hasUpper && !hasLower) {
                    char = '▀';
                }
                else if (!hasUpper && hasLower) {
                    char = '▄';
                }
                const charString = color ? colorize(char, color, 'foreground') : char;
                const styled = toStyledCharacters(charString);
                if (styled.length > 0) {
                    let bgColor = styled.getBgColor(0);
                    if (getExistingChar && (char === '▀' || char === '▄')) {
                        const existing = getExistingChar(drawX, drawY);
                        if (existing?.bgColor) {
                            bgColor = existing.bgColor;
                        }
                    }
                    setChar(drawX, drawY, styled.getValue(0), styled.getFormatFlags(0), styled.getFgColor(0), bgColor, styled.getLink(0));
                }
            }
        }
    }
    else {
        for (let i = startIndex; i < endIndex; i++) {
            const drawX = x + i;
            const drawY = y;
            if (drawY >= clip.y &&
                drawY < clip.y + clip.h &&
                drawX >= clip.x &&
                drawX < clip.x + clip.w) {
                let char = '█';
                const hasLeft = 2 * i >= thumbStartHalf && 2 * i < thumbEndHalf;
                const hasRight = 2 * i + 1 >= thumbStartHalf && 2 * i + 1 < thumbEndHalf;
                if (hasLeft && !hasRight) {
                    char = '▌';
                }
                else if (!hasLeft && hasRight) {
                    char = '▐';
                }
                const charString = color ? colorize(char, color, 'foreground') : char;
                const styled = toStyledCharacters(charString);
                if (styled.length > 0) {
                    let bgColor = styled.getBgColor(0);
                    if (getExistingChar && (char === '▌' || char === '▐')) {
                        const existing = getExistingChar(drawX, drawY);
                        if (existing?.bgColor) {
                            bgColor = existing.bgColor;
                        }
                    }
                    setChar(drawX, drawY, styled.getValue(0), styled.getFormatFlags(0), styled.getFgColor(0), bgColor, styled.getLink(0));
                }
            }
        }
    }
};
//# sourceMappingURL=render-scrollbar.js.map