// @flow
import { NARROW_SPACE, WIDE_SPACE, NARROW_BAR, WIDE_BAR } from "../Core/Characters"

export function rect(x: number, height: number, width: number, filled: boolean): string {
	return `<rect x="${x}" y="0" width="${width}" height="${height}" fill="${filled ? "black" : "white"}"/>`
}

export function renderBarcodeToSVG(input: string, height: number, baseWidth: number): string {
	let svg = ""
	let x = 0

	for (const char of input) {
		switch (char) {
			case NARROW_SPACE:
				svg += rect(x, height, baseWidth, false)
				x += baseWidth
				break
			case WIDE_SPACE:
				svg += rect(x, height, baseWidth * 3, false)
				x += baseWidth * 3
				break
			case NARROW_BAR:
				svg += rect(x, height, baseWidth, true)
				x += baseWidth
				break
			case WIDE_BAR:
				svg += rect(x, height, baseWidth * 3, true)
				x += baseWidth * 3
				break
		}
	}

	return `<svg viewBox="0 0 ${x} ${height}">${svg}</svg>`
}

// @flow
import { NARROW_SPACE, WIDE_SPACE, NARROW_BAR, WIDE_BAR } from "../Core/Characters"

const NARROW_WIDTH = 1
const WIDE_WIDTH = 3

export function rect(x: number, wide: boolean, filled: boolean): string {
	return `<rect x="${x}" y="0" width="${wide ? WIDE_WIDTH : NARROW_WIDTH}" height="1" fill="${filled ? "black" : "white"}"/>`
}

export function renderBarcodeToSVG(input: string): string {
	let svg = ""
	let x = 0

	for (const char of input) {
		switch (char) {
			case NARROW_SPACE:
				svg += rect(x, false, false)
				x += NARROW_WIDTH
				break
			case WIDE_SPACE:
				svg += rect(x, true, false)
				x += WIDE_WIDTH
				break
			case NARROW_BAR:
				svg += rect(x, false, true)
				x += NARROW_WIDTH
				break
			case WIDE_BAR:
				svg += rect(x, true, true)
				x += WIDE_WIDTH
				break
		}
	}

	return `<svg viewBox="0 0 ${x} 1">${svg}</svg>`
}

Make SVG dimensions normalized and remove width and height attrs.
Make SVG dimensions normalized and remove width and height attrs.
