








import {
  createEmployeePackageImportTransactionStore,
  PackageImportTransactionError,
} from "./uutkwvuoermosbzjabjy.mjs";

const ACCOUNT_ID_PATTERN = /^[A-Z2-9]{4}-[A-Z2-9]{4}-[A-Z2-9]{4}$/;

export { PackageImportTransactionError as EmployeeAssignmentImportTransactionError };

function normalizeAccountId(value) {
  const normalized = String(value || "").trim().toUpperCase();
  return ACCOUNT_ID_PATTERN.test(normalized) ? normalized : "";
}

function invalidAccount() {
  throw new PackageImportTransactionError("PACKAGE_IMPORT_ACCOUNT", { statusCode: 403 });
}

function mapPublicTransaction(transaction) {
  if (!transaction || typeof transaction !== "object") return transaction;
  const {
    accountId,
    centralAccountId,
    ...rest
  } = transaction;
  return {
    ...rest,
    sourceAccountId: accountId,
    targetAccountId: centralAccountId,
  };
}

export function createEmployeeAssignmentImportTransactionStore(options = {}) {
  const baseStore = createEmployeePackageImportTransactionStore(options);

  async function stage({
    sourceAccountId,
    targetAccountId,
    baseDigest,
    sourcePackageRoot,
    manifest,
    database,
  }) {
    const normalizedSource = normalizeAccountId(sourceAccountId);
    const normalizedTarget = normalizeAccountId(targetAccountId);
    if (
      !normalizedSource
      || !normalizedTarget
      || manifest?.sourceAccountId !== normalizedSource
      || manifest?.targetAccountId !== normalizedTarget
      || database?.centralAccountId !== normalizedSource
      || database?.accountId !== normalizedTarget
    ) {
      invalidAccount();
    }
    
    
    
    const stagingDatabase = {
      ...database,
      accountId: normalizedSource,
      targetAccountId: normalizedTarget,
    };
    return mapPublicTransaction(await baseStore.stage({
      centralAccountId: normalizedTarget,
      sourceAccountId: normalizedSource,
      kind: "central-to-employee",
      targetAccountId: normalizedTarget,
      baseDigest,
      sourcePackageRoot,
      manifest,
      database: stagingDatabase,
    }));
  }

  async function apply({ targetAccountId, sourceAccountId, ...binding }) {
    const normalizedTarget = normalizeAccountId(targetAccountId);
    const normalizedSource = normalizeAccountId(sourceAccountId);
    if (!normalizedTarget || !normalizedSource) invalidAccount();
    return mapPublicTransaction(await baseStore.apply({
      centralAccountId: normalizedTarget,
      kind: "central-to-employee",
      sourceAccountId: normalizedSource,
      targetAccountId: normalizedTarget,
      ...binding,
    }));
  }

  async function getPending({ targetAccountId }) {
    const normalizedTarget = normalizeAccountId(targetAccountId);
    if (!normalizedTarget) invalidAccount();
    const pending = await baseStore.getPending({
      centralAccountId: normalizedTarget,
    });
    return pending.map(mapPublicTransaction);
  }

  async function confirm({ targetAccountId, sourceAccountId, ...binding }) {
    const normalizedTarget = normalizeAccountId(targetAccountId);
    const normalizedSource = normalizeAccountId(sourceAccountId);
    if (!normalizedTarget || !normalizedSource) invalidAccount();
    return baseStore.confirm({
      centralAccountId: normalizedTarget,
      kind: "central-to-employee",
      sourceAccountId: normalizedSource,
      targetAccountId: normalizedTarget,
      ...binding,
    });
  }

  async function rollback({ targetAccountId, sourceAccountId, ...binding }) {
    const normalizedTarget = normalizeAccountId(targetAccountId);
    const normalizedSource = normalizeAccountId(sourceAccountId);
    if (!normalizedTarget || !normalizedSource) invalidAccount();
    return baseStore.rollback({
      centralAccountId: normalizedTarget,
      kind: "central-to-employee",
      sourceAccountId: normalizedSource,
      targetAccountId: normalizedTarget,
      ...binding,
    });
  }

  return {
    stage,
    apply,
    getPending,
    confirm,
    rollback,
  };
}
