import { C as Container, a as Ctx, $ as OrderStatus, T as Trans, F as FeUtils, ab as PurchaseHelper, g as AccPath, L as Loading, i as CommonError } from "./index.js";
import { P as Pagination } from "./Pagination.js";
import { w as withPropsGuard } from "./WithPropsGuard.js";
import React__default from "react";
import { Link } from "react-router-dom";
import { A as AccountMainSection, b as AccountMainSectionHeader, a as AccountMainSectionBody } from "./AccountMainSection.js";
import "./validator.js";
import "./dateFns.js";
import "./reactPlayer.js";
import "./reactSlick.js";
class OrderListLogic extends Container {
  constructor(props) {
    super();
    this.props = props;
    this.state = {
      isFetching: false,
      orderInfoList: null,
      error: null
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  getLimit() {
    const pageSize = 10;
    return this.props.pageSize || pageSize;
  }
  getOffset(page) {
    return page ? (page - 1) * this.getLimit() : 0;
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: true,
        orderInfoList: null,
        error: null
      });
      const { page, userId, status } = this.props;
      if (!userId)
        return;
      const orderInfoList = await this.sdk.Platform.Order().getOrderList({
        userId,
        queryParams: { status, offset: this.getOffset(page), limit: this.getLimit() }
      }).then((result) => {
        if (result.error)
          throw result.error;
        return result.response.data;
      });
      await this.setState({
        orderInfoList
      });
    } catch (error) {
      this.setState({ error });
    } finally {
      await this.setState({ isFetching: false });
    }
  }
}
const orderHistoryPage = "orderHistoryPage _orderHistoryPage_1c1c4";
var styles = {
  orderHistoryPage
};
const OrderStatusTranslation = (props) => {
  const { status } = props;
  switch (true) {
    case status === OrderStatus.enum.INIT:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.init"
      }, "Initialized");
    case status === OrderStatus.enum.CHARGEBACK:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.chargeback"
      }, "Chargeback");
    case status === OrderStatus.enum.CHARGEBACK_REVERSED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.chargebackReversed"
      }, "Chargeback Reversed");
    case status === OrderStatus.enum.CHARGED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.charged"
      }, "Charged");
    case status === OrderStatus.enum.FULFILLED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.fulfilled"
      }, "Fulfilled");
    case status === OrderStatus.enum.FULFILL_FAILED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.fulfillFailed"
      }, "Fulfill Failed");
    case status === OrderStatus.enum.REFUNDING:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.refunding"
      }, "Refunding");
    case status === OrderStatus.enum.REFUNDED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.refunded"
      }, "Refunded");
    case status === OrderStatus.enum.REFUND_FAILED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.refundFailed"
      }, "Refund Failed");
    case status === OrderStatus.enum.CLOSED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.closed"
      }, "Closed");
    case status === OrderStatus.enum.DELETED:
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderStatus.deleted"
      }, "Deleted");
    default:
      return /* @__PURE__ */ React__default.createElement("span", null, "status");
  }
};
class OrderHistoryPage extends React__default.Component {
  constructor(props) {
    var _a;
    super(props);
    this.renderTableOrder = (orderList) => {
      return /* @__PURE__ */ React__default.createElement("div", {
        className: styles.orderHistoryPage
      }, /* @__PURE__ */ React__default.createElement("div", {
        className: "tableHead"
      }, /* @__PURE__ */ React__default.createElement("div", {
        className: "tableColumn first"
      }, /* @__PURE__ */ React__default.createElement("p", {
        className: "title"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderHistoryPage.date"
      }, "Date")), /* @__PURE__ */ React__default.createElement("p", {
        className: "title"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderHistoryPage.itemName"
      }, "Item name"))), /* @__PURE__ */ React__default.createElement("div", {
        className: "tableColumn second"
      }, /* @__PURE__ */ React__default.createElement("p", {
        className: "title"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderHistoryPage.total"
      }, "Total")), /* @__PURE__ */ React__default.createElement("p", {
        className: "title"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "OrderHistoryPage.status"
      }, "Status")))), orderList.map((order) => {
        const createdDate = FeUtils.formatDate(order.createdTime, "LLLL");
        return /* @__PURE__ */ React__default.createElement("div", {
          className: "tableContent",
          key: order.orderNo
        }, /* @__PURE__ */ React__default.createElement("div", {
          className: "tableColumn first"
        }, /* @__PURE__ */ React__default.createElement("div", {
          className: "date"
        }, /* @__PURE__ */ React__default.createElement("p", null, createdDate)), /* @__PURE__ */ React__default.createElement("div", {
          className: "textContent "
        }, order.itemSnapshot && order.itemSnapshot.title)), /* @__PURE__ */ React__default.createElement("div", {
          className: "tableColumn second"
        }, /* @__PURE__ */ React__default.createElement("div", {
          className: "textContent"
        }, `${order.currency.currencySymbol} ${PurchaseHelper.calculateIntegerPrice(order.totalPrice || 0, order.currency)}`), /* @__PURE__ */ React__default.createElement("div", {
          className: "textContent"
        }, /* @__PURE__ */ React__default.createElement(OrderStatusTranslation, {
          status: order.status
        })), /* @__PURE__ */ React__default.createElement("div", {
          className: "btnView"
        }, /* @__PURE__ */ React__default.createElement(Link, {
          to: AccPath.OrderHistoryDetail.makePath({
            orderNo: order.orderNo
          })
        }, /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "OrderHistoryPage.view"
        }, "View")))));
      }));
    };
    this.renderPagination = () => {
      const { orderInfoList } = this.logic.state;
      if (!orderInfoList)
        return;
      const { paging } = orderInfoList;
      const hasNext = !!(paging == null ? void 0 : paging.next);
      const hasPrev = !!(paging == null ? void 0 : paging.previous);
      const currentPage = this.props.page;
      return /* @__PURE__ */ React__default.createElement("div", {
        className: "paginationContainer"
      }, /* @__PURE__ */ React__default.createElement(Pagination, {
        next: hasNext && AccPath.OrderHistory.withSearchParams({ page: currentPage + 1 }),
        prev: hasPrev && AccPath.OrderHistory.withSearchParams({ page: currentPage - 1 })
      }));
    };
    const { page } = props;
    const userId = ((_a = Ctx.getAppState().getUser()) == null ? void 0 : _a.userId) || null;
    this.logic = new OrderListLogic({ page, userId });
    this.logic.subscribe(() => this.setState({}));
  }
  componentDidMount() {
    this.logic.init();
  }
  render() {
    const { isFetching, orderInfoList, error } = this.logic.state;
    return /* @__PURE__ */ React__default.createElement(AccountMainSection, {
      className: styles.orderHistoryPage
    }, /* @__PURE__ */ React__default.createElement(AccountMainSectionHeader, null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "OrderHistoryPage.title"
    }, "Order History")), /* @__PURE__ */ React__default.createElement(AccountMainSectionBody, null, isFetching && /* @__PURE__ */ React__default.createElement(Loading, {
      className: "loading"
    }), !isFetching && error && /* @__PURE__ */ React__default.createElement("div", {
      className: "errorContainer"
    }, /* @__PURE__ */ React__default.createElement(CommonError, {
      error
    })), !isFetching && orderInfoList && orderInfoList.data.length > 0 && this.renderTableOrder(orderInfoList.data), !isFetching && orderInfoList && orderInfoList.data.length === 0 && /* @__PURE__ */ React__default.createElement("div", {
      className: "noData"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "OrderHistoryPage.dataNotFound"
    }, "Order history data not found")), !isFetching && orderInfoList && this.renderPagination()));
  }
}
const OrderHistoryPageWithPropsGuard = withPropsGuard(OrderHistoryPage, (prevProps, nextProps) => {
  return prevProps.page !== nextProps.page;
});
export { OrderHistoryPageWithPropsGuard as default };
