import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import cn from "classnames";
import React from "react";
import styled from "styled-components";
import { safeToFixed, safeToPercent } from "../../format";
import Text from "../text";
const arr10 = Array.from(new Array(10));
const Wrapper = styled.div `
  width: 224px;
  background: #f9fcff;
  height: 100%;

  .tabs {
    display: flex;
    flex-flow: row nowrap;
    border-bottom: 1px #ccdae9 solid;
    margin-bottom: 6px;
    padding-top: 12px;

    p {
      cursor: pointer;
      padding-bottom: 8px;
      margin-left: 16px;

      &.active {
        color: #1792ff;
        border-bottom: 2px #1792ff solid;
      }
    }
  }

  .orders {
    .header {
      display: flex;
      flex-flow: row nowrap;

      p {
        width: 26%;
        text-align: right;

        :nth-of-type(1) {
          width: 45%;
        }

        span {
          background: #ccdae9;
          padding: 2px;
          margin: 2px;
        }
      }
    }

    .spread {
      display: flex;
      flex-flow: row nowrap;
      border-top: 1px solid #ccdae9;
      border-bottom: 1px solid #ccdae9;
      margin-top: 4px;
      margin-bottom: 4px;
      padding: 8px 0;

      .title {
        margin-left: 60px;
        margin-right: 15px;
      }

      .value {
        margin-left: 8px;
      }
    }

    .item {
      cursor: pointer;
      width: 100%;
      display: flex;
      flex-flow: row nowrap;

      .bar {
        width: 20%;
        margin-bottom: 2px;
        height: 16px;

        .inner {
          height: 16px;

          .mine {
            height: 16px;
          }
        }
      }

      .amount {
        width: 25%;
        p {
          text-align: right;
        }
      }

      &.sell {
        .bar {
          .inner {
            background: rgba(254, 102, 102, 0.4);

            .mine {
              background: rgba(254, 102, 102, 0.4);
            }
          }
        }
      }

      &.buy {
        .bar {
          .inner {
            background: rgba(85, 174, 88, 0.4);
          }

          .mine {
            background: rgba(85, 174, 88, 0.4);
          }
        }
      }
    }
  }
`;
const Orderbook = ({ buy, sell, from, to, onBarClick, }) => {
    const size = React.useMemo(() => {
        return {
            buy: buy.reduce((prev, { size }) => (prev > size ? prev : size), 0),
            sell: sell.reduce((prev, { size }) => (prev > size ? prev : size), 0),
        };
    }, [buy, sell]);
    const sellSorted = React.useMemo(() => [...sell].sort((b, a) => a.price - b.price).slice(-10), [sell]);
    const buySorted = React.useMemo(() => [...buy].sort((b, a) => a.price - b.price).slice(0, 10), [buy]);
    return (_jsxs(Wrapper, { children: [_jsxs("div", { className: "tabs", children: [_jsx(Text.T4, { className: "active", children: "Orders" }), _jsx(Text.T4, { children: "Trades" })] }), _jsxs("div", { className: "orders", children: [_jsxs("div", { className: "header", children: [_jsxs(Text.F3, { children: ["Size", _jsx("span", { children: from.toUpperCase() })] }), _jsxs(Text.F3, { children: ["Price", _jsx("span", { children: to.toUpperCase() })] }), _jsx(Text.F3, { style: { paddingRight: 10 }, children: "Mine" })] }), arr10.map((_, i) => {
                        const index = sellSorted.length - 10 + i;
                        if (index < 0) {
                            return (_jsx("div", { className: cn("item", "sell"), children: _jsx("div", { className: "bar" }) }, `sell:i:${i}`));
                        }
                        const e = sellSorted[index];
                        return (_jsxs("div", { className: cn("item", "sell"), onClick: () => onBarClick?.(e), children: [_jsx("div", { className: "bar", children: _jsx("div", { className: "inner", style: {
                                            width: safeToPercent(e.size / size.sell, 0),
                                        }, children: e.mine ? (_jsx("div", { className: "mine", style: {
                                                width: safeToPercent(e.mine / e.size, 0),
                                            } })) : undefined }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: safeToFixed(e.size, 0, true) }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: safeToFixed(e.price, e.price > 10 ? 2 : 4, true) }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: e.mine ? safeToFixed(e.mine, 0) : "-" }) })] }, `sell:${e.price}`));
                    }), _jsxs("div", { className: "spread", children: [_jsx(Text.F2, { className: "title", children: "Spread" }), _jsx(Text.F2, { className: "value", children: safeToFixed((sellSorted.slice(-1)[0]?.price ?? 0) -
                                    (buySorted[0]?.price ?? 0), 4, true) })] }), buySorted.map((e) => {
                        return (_jsxs("div", { className: cn("item", "buy"), onClick: () => onBarClick?.(e), children: [_jsx("div", { className: "bar", children: _jsx("div", { className: "inner", style: {
                                            width: safeToPercent(e.size / size.sell, 0),
                                        }, children: e.mine ? (_jsx("div", { className: "mine", style: {
                                                width: safeToPercent(e.mine / e.size, 0),
                                            } })) : undefined }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: safeToFixed(e.size, 0, true) }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: safeToFixed(e.price, e.price > 10 ? 2 : 4, true) }) }), _jsx("div", { className: "amount", children: _jsx(Text.F2, { children: e.mine ? safeToFixed(e.mine, 0) : "-" }) })] }, `buy:${e.price}`));
                    })] })] }));
};
export default Orderbook;
//# sourceMappingURL=index.js.map