// Copyright 2017-2025 @pezkuwi/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ContractMetadataV2, ContractMetadataV3 } from '@pezkuwi/types/interfaces';
import type { Registry } from '@pezkuwi/types/types';

import { objectSpread } from '@pezkuwi/util';

export function v2ToV3 (registry: Registry, v2: ContractMetadataV2): ContractMetadataV3 {
  return registry.createType('ContractMetadataV3', objectSpread({}, v2, {
    spec: objectSpread({}, v2.spec, {
      constructors: v2.spec.constructors.map((c) =>
        // V3 introduces the payable flag on constructors, for <V3, it is always true
        registry.createType('ContractConstructorSpecV3', objectSpread({}, c, { payable: true }))
      )
    })
  }));
}
