"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShoppingCart = void 0;
const react_web_components_1 = require("@nimles/react-web-components");
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const react_redux_2 = require("@nimles/react-redux");
const react_web_components_2 = require("@nimles/react-web-components");
const styled_1 = __importDefault(require("@emotion/styled"));
const TotalAmount = styled_1.default.div `
  padding-top: 10px;
  padding-bottom: 10px;
  font-weight: bold;
  font-size: 20px;
`;
const ShoppingCartItem = ({ cart, item, isEditable }) => {
    const dispatch = (0, react_redux_1.useDispatch)();
    const [prelQuantity, setPrelQuantity] = (0, react_1.useState)();
    const [savedQuantity, setSavedQuantity] = (0, react_1.useState)(item.quantity);
    const [isUpdating, setUpdating] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        (function syncCartItem() {
            return __awaiter(this, void 0, void 0, function* () {
                if (!isUpdating) {
                    if (prelQuantity !== undefined && prelQuantity != savedQuantity) {
                        try {
                            setUpdating(true);
                            yield dispatch((0, react_redux_2.updateCartItem)(cart.id, {
                                productId: item.productId,
                                quantity: prelQuantity,
                            }));
                            setSavedQuantity(prelQuantity);
                        }
                        catch (e) {
                            setPrelQuantity(undefined);
                        }
                        finally {
                            setUpdating(false);
                        }
                    }
                    else {
                        setPrelQuantity(undefined);
                        setSavedQuantity(undefined);
                    }
                }
            });
        })();
    }, [prelQuantity, isUpdating]);
    const changeCartItemQuantity = (item, e) => __awaiter(void 0, void 0, void 0, function* () {
        e.preventDefault();
        const value = Number(e.target.value);
        setPrelQuantity(value);
    });
    return (<react_web_components_1.ListItem>
      <react_web_components_1.Row wrap align="center">
        <react_web_components_1.Column flex>{item.product && item.product.name}</react_web_components_1.Column>
        <react_web_components_1.Column xs={60} md={15} align="flex-end">
          <react_web_components_2.Price>{item.product && item.product.netPrice}</react_web_components_2.Price>
        </react_web_components_1.Column>
        <react_web_components_1.Column width="120px">
          {isEditable ? (<react_web_components_1.Input align="right" type="number" value={prelQuantity || item.quantity} onChange={(e) => changeCartItemQuantity(item, e)}/>) : (item.quantity)}
        </react_web_components_1.Column>
        <react_web_components_1.Column xs={20} md={15} align="flex-end">
          <react_web_components_2.Price>{item.product && item.product.netPrice * item.quantity}</react_web_components_2.Price>
        </react_web_components_1.Column>
      </react_web_components_1.Row>
    </react_web_components_1.ListItem>);
};
const ShoppingCart = ({ cart, isEditable }) => {
    const cartItems = cart &&
        cart.items &&
        cart.items.map((item) => {
            return Object.assign({}, item);
        });
    const totalAmount = cartItems &&
        cartItems.reduce((sum, item) => sum + item.product.netPrice * item.quantity, 0);
    return (<>
      <react_web_components_1.List>
        {cartItems &&
            cartItems.map((item, index) => (<ShoppingCartItem key={index} cart={cart} item={item} isEditable={isEditable}/>))}
      </react_web_components_1.List>
      <react_web_components_2.Divider />
      <react_web_components_1.Row justify="flex-end">
        <react_web_components_1.Column padding>
          <TotalAmount>
            <react_web_components_2.Price>{totalAmount}</react_web_components_2.Price>
          </TotalAmount>
        </react_web_components_1.Column>
      </react_web_components_1.Row>
    </>);
};
exports.ShoppingCart = ShoppingCart;
//# sourceMappingURL=ShoppingCart.jsx.map