import React, { forwardRef } from "react";
import { Container, Icon, Item, Key, KeyValuePair, Value, } from "./KeyValue.styled.js";
const DEFAULT_COLS = 2;
const KeyValue = forwardRef((props, ref) => {
    var _a;
    const cols = (_a = props.cols) !== null && _a !== void 0 ? _a : DEFAULT_COLS;
    const items = props.items.map((value, key) => {
        const width = `${100 / cols}%`;
        const style = { width };
        const isFirstRow = key < cols;
        const isFirstCol = (key % cols) === 0;
        const kvKey = value.key != null ? React.createElement(Key, null, value.key) : null;
        const kvValue = value.value != null ? React.createElement(Value, null, value.value) : null;
        const keyValue = (React.createElement(KeyValuePair, null,
            props.valueFirst ? kvValue : kvKey,
            !props.valueFirst ? kvValue : kvKey));
        const icon = value.icon != null ? React.createElement(Icon, null, value.icon) : null;
        const component = value.onClick ? "button" : "div";
        return React.createElement(Item, {
            key,
            style,
            as: component,
            notFirstRow: !isFirstRow,
            notFirstCol: !isFirstCol,
        }, React.createElement(React.Fragment, null,
            icon,
            keyValue));
    });
    const rest = items.length % cols;
    const missingItems = rest ? cols - rest : 0;
    if (missingItems) {
        const width = `${(100 / cols) * (missingItems)}%`;
        const style = { width };
        const firstRow = items.length < cols;
        const firstCol = (items.length % cols) === 0;
        items.push(React.createElement(Item, { key: "extra", css: style, notFirstCol: !firstCol, notFirstRow: !firstRow }));
    }
    return (React.createElement(Container, { className: props.className, ref: ref }, items));
});
KeyValue.displayName = "KeyValue";
KeyValue.toString = () => Container.toString();
const KeyValueItemSelector = Item.toString();
const KeyValuePairSelector = KeyValuePair.toString();
const KeyValueKeySelector = Key.toString();
const KeyValueValueSelector = Value.toString();
const KeyValueIconSelector = Icon.toString();
export { KeyValue, KeyValueItemSelector, KeyValuePairSelector, KeyValueKeySelector, KeyValueValueSelector, KeyValueIconSelector, };
//# sourceMappingURL=KeyValue.js.map