// SPDX-License-Identifier: Apache-2.0
import { DelegateContractId, ContractId as HContractId, Long } from "@hiero-ledger/sdk";
import { proto } from "@hashgraph/proto";
import InvalidKeyForContract from "./error/InvalidKeyForContract";
import CheckStrings from "../../../core/checks/strings/CheckStrings";
import { InvalidContractId } from "./error/InvalidContractId";
import { HederaId } from "../shared/HederaId";
import LogService from "../../../app/service/log/LogService";
export default class ContractId extends HederaId {
    constructor(value) {
        if (value.length == 42 && value.startsWith("0x")) {
            throw new InvalidContractId(value);
        }
        super(value);
    }
    static fromProtoBufKey(key, options = { strict: false }) {
        const normalizedInput = key.replace(/\s/g, "");
        const normalizedHexInput = normalizedInput.replace(/0x/g, "").toLowerCase();
        const keyProto = Buffer.from(normalizedHexInput, "hex");
        const out = proto.Key.decode(keyProto);
        let id = out?.contractID?.contractNum || out?.delegatableContractId?.contractNum;
        if (options.strict && !id) {
            throw new InvalidKeyForContract(out);
        }
        else if (!id) {
            id = Long.ZERO;
        }
        return new ContractId("0.0." + id.toString());
    }
    static fromHederaContractId(con) {
        return new ContractId(String(con));
    }
    static fromHederaEthereumAddress(evmAddress) {
        return new ContractId(HContractId.fromSolidityAddress(evmAddress).toString());
    }
    static validate(id) {
        const err = [];
        if (!CheckStrings.isNotEmpty(id)) {
            err.push(new InvalidContractId(id));
        }
        else {
            try {
                if (!(id.length == 42 && id.startsWith("0x")))
                    HContractId.fromString(id);
            }
            catch (error) {
                LogService.logError(error);
                err.push(new InvalidContractId(id));
            }
        }
        return err;
    }
    toDelegateContractId() {
        return DelegateContractId.fromString(this.value);
    }
    toString() {
        return this.value;
    }
}
