import { asFunction, asValue } from "awilix";
import { registerOptimisticLockReaders } from "@open-mercato/shared/lib/crud/optimistic-lock-store";
import { DefaultSalesCalculationService } from "./services/salesCalculationService.js";
import { DefaultTaxCalculationService } from "./services/taxCalculationService.js";
import { SalesDocumentNumberGenerator } from "./services/salesDocumentNumberGenerator.js";
import { DefaultSalesOrderService } from "./services/salesOrderService.js";
import {
  SalesOrder,
  SalesOrderLine,
  SalesOrderAdjustment,
  SalesQuote,
  SalesQuoteLine,
  SalesQuoteAdjustment,
  SalesChannel,
  SalesShipment,
  SalesShipmentItem,
  SalesInvoice,
  SalesInvoiceLine,
  SalesCreditMemo,
  SalesCreditMemoLine,
  SalesPayment,
  SalesPaymentAllocation,
  SalesReturn,
  SalesReturnLine,
  SalesNote,
  SalesDocumentAddress,
  SalesDocumentTag,
  SalesDocumentTagAssignment,
  SalesShippingMethod,
  SalesDeliveryWindow,
  SalesPaymentMethod,
  SalesTaxRate
} from "./data/entities.js";
const RESOURCE_KIND_ORDER = "sales.order";
const RESOURCE_KIND_PAYMENT = "sales.payment";
const RESOURCE_KIND_SHIPMENT = "sales.shipment";
async function readOrderUpdatedAtById(em, orderId, tenantId, organizationId) {
  if (!orderId) return null;
  const row = await em.findOne(
    SalesOrder,
    {
      id: orderId,
      tenantId,
      ...organizationId ? { organizationId } : {},
      deletedAt: null
    },
    { fields: ["updatedAt"] }
  );
  return row?.updatedAt instanceof Date ? row.updatedAt.toISOString() : null;
}
const readSalesOrderUpdatedAt = async (em, { resourceId, tenantId, organizationId }) => readOrderUpdatedAtById(em, resourceId, tenantId, organizationId);
const readPaymentParentOrderUpdatedAt = async (em, { resourceId, tenantId, organizationId }) => {
  const payment = await em.findOne(
    SalesPayment,
    {
      id: resourceId,
      tenantId,
      ...organizationId ? { organizationId } : {},
      deletedAt: null
    },
    { fields: ["order"] }
  );
  const orderId = payment?.order;
  const resolvedOrderId = typeof orderId === "string" ? orderId : orderId?.id ?? null;
  return readOrderUpdatedAtById(em, resolvedOrderId, tenantId, organizationId);
};
const readShipmentParentOrderUpdatedAt = async (em, { resourceId, tenantId, organizationId }) => {
  const shipment = await em.findOne(
    SalesShipment,
    {
      id: resourceId,
      tenantId,
      ...organizationId ? { organizationId } : {},
      deletedAt: null
    },
    { fields: ["order"] }
  );
  const orderId = shipment?.order;
  const resolvedOrderId = typeof orderId === "string" ? orderId : orderId?.id ?? null;
  return readOrderUpdatedAtById(em, resolvedOrderId, tenantId, organizationId);
};
registerOptimisticLockReaders({
  [RESOURCE_KIND_ORDER]: readSalesOrderUpdatedAt,
  [RESOURCE_KIND_PAYMENT]: readPaymentParentOrderUpdatedAt,
  [RESOURCE_KIND_SHIPMENT]: readShipmentParentOrderUpdatedAt
});
function register(container) {
  container.register({
    salesCalculationService: asFunction(({ eventBus }) => {
      return new DefaultSalesCalculationService(eventBus ?? null);
    }).singleton().proxy(),
    taxCalculationService: asFunction(({ em, eventBus }) => {
      return new DefaultTaxCalculationService(em, eventBus ?? null);
    }).singleton().proxy(),
    salesDocumentNumberGenerator: asFunction(({ em }) => {
      return new SalesDocumentNumberGenerator(em);
    }).singleton().proxy(),
    salesOrderService: asFunction(({ em }) => {
      return new DefaultSalesOrderService(em);
    }).singleton().proxy(),
    SalesOrder: asValue(SalesOrder),
    SalesOrderLine: asValue(SalesOrderLine),
    SalesOrderAdjustment: asValue(SalesOrderAdjustment),
    SalesQuote: asValue(SalesQuote),
    SalesQuoteLine: asValue(SalesQuoteLine),
    SalesQuoteAdjustment: asValue(SalesQuoteAdjustment),
    SalesChannel: asValue(SalesChannel),
    SalesShipment: asValue(SalesShipment),
    SalesShipmentItem: asValue(SalesShipmentItem),
    SalesInvoice: asValue(SalesInvoice),
    SalesInvoiceLine: asValue(SalesInvoiceLine),
    SalesCreditMemo: asValue(SalesCreditMemo),
    SalesCreditMemoLine: asValue(SalesCreditMemoLine),
    SalesPayment: asValue(SalesPayment),
    SalesPaymentAllocation: asValue(SalesPaymentAllocation),
    SalesReturn: asValue(SalesReturn),
    SalesReturnLine: asValue(SalesReturnLine),
    SalesNote: asValue(SalesNote),
    SalesDocumentAddress: asValue(SalesDocumentAddress),
    SalesDocumentTag: asValue(SalesDocumentTag),
    SalesDocumentTagAssignment: asValue(SalesDocumentTagAssignment),
    SalesShippingMethod: asValue(SalesShippingMethod),
    SalesDeliveryWindow: asValue(SalesDeliveryWindow),
    SalesPaymentMethod: asValue(SalesPaymentMethod),
    SalesTaxRate: asValue(SalesTaxRate)
  });
}
export {
  register
};
//# sourceMappingURL=di.js.map
