// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { plainToInstance, Type } from 'class-transformer';
/**
 * Address type variants.
 */
var AddressType;
(function (AddressType) {
    /** An Ed25519 address. */
    AddressType[AddressType["Ed25519"] = 0] = "Ed25519";
    /** An Account address. */
    AddressType[AddressType["Account"] = 8] = "Account";
    /** An NFT address. */
    AddressType[AddressType["Nft"] = 16] = "Nft";
    /** An Anchor address. */
    AddressType[AddressType["Anchor"] = 24] = "Anchor";
    /** An implicit account creation address. */
    AddressType[AddressType["ImplicitAccountCreation"] = 32] = "ImplicitAccountCreation";
    /** A Multi address. */
    AddressType[AddressType["Multi"] = 40] = "Multi";
    /** An address with restricted capabilities. */
    AddressType[AddressType["Restricted"] = 48] = "Restricted";
})(AddressType || (AddressType = {}));
/**
 * The base class for addresses.
 */
var Address = /** @class */ (function () {
    /**
     * @param type The type of the address.
     */
    function Address(type) {
        this.type = type;
    }
    /**
     * Parse an address from a JSON string.
     */
    Address.parse = function (data) {
        if (data.type == AddressType.Ed25519) {
            return plainToInstance(Ed25519Address, data);
        }
        else if (data.type == AddressType.Account) {
            return plainToInstance(AccountAddress, data);
        }
        else if (data.type == AddressType.Nft) {
            return plainToInstance(NftAddress, data);
        }
        else if (data.type == AddressType.Anchor) {
            return plainToInstance(AnchorAddress, data);
        }
        else if (data.type == AddressType.ImplicitAccountCreation) {
            return plainToInstance(ImplicitAccountCreationAddress, data);
        }
        else if (data.type == AddressType.Restricted) {
            return plainToInstance(RestrictedAddress, data);
        }
        throw new Error('Invalid JSON');
    };
    return Address;
}());
/**
 * An Ed25519 Address.
 */
var Ed25519Address = /** @class */ (function (_super) {
    __extends(Ed25519Address, _super);
    /**
     * @param pubKeyHash BLAKE2b-256 hash of an Ed25519 public key as hex-encoded string.
     */
    function Ed25519Address(pubKeyHash) {
        var _this = _super.call(this, AddressType.Ed25519) || this;
        _this.pubKeyHash = pubKeyHash;
        return _this;
    }
    Ed25519Address.prototype.toString = function () {
        return this.pubKeyHash;
    };
    return Ed25519Address;
}(Address));
/**
 * An Account address.
 */
var AccountAddress = /** @class */ (function (_super) {
    __extends(AccountAddress, _super);
    /**
     * @param address An account address as account ID.
     */
    function AccountAddress(address) {
        var _this = _super.call(this, AddressType.Account) || this;
        _this.accountId = address;
        return _this;
    }
    AccountAddress.prototype.toString = function () {
        return this.accountId;
    };
    return AccountAddress;
}(Address));
/**
 * An NFT address.
 */
var NftAddress = /** @class */ (function (_super) {
    __extends(NftAddress, _super);
    /**
     * @param address An NFT address as NFT ID.
     */
    function NftAddress(address) {
        var _this = _super.call(this, AddressType.Nft) || this;
        _this.nftId = address;
        return _this;
    }
    NftAddress.prototype.toString = function () {
        return this.nftId;
    };
    return NftAddress;
}(Address));
/**
 * An Anchor address.
 */
var AnchorAddress = /** @class */ (function (_super) {
    __extends(AnchorAddress, _super);
    /**
     * @param address An anchor address as anchor ID.
     */
    function AnchorAddress(address) {
        var _this = _super.call(this, AddressType.Anchor) || this;
        _this.anchorId = address;
        return _this;
    }
    AnchorAddress.prototype.toString = function () {
        return this.anchorId;
    };
    return AnchorAddress;
}(Address));
/**
 * An implicit account creation address that can be used to convert a Basic Output to an Account Output.
 */
var ImplicitAccountCreationAddress = /** @class */ (function (_super) {
    __extends(ImplicitAccountCreationAddress, _super);
    /**
     * @param pubKeyHash BLAKE2b-256 hash of an Ed25519 public key as hex-encoded string.
     */
    function ImplicitAccountCreationAddress(pubKeyHash) {
        var _this = _super.call(this, AddressType.ImplicitAccountCreation) || this;
        _this.pubKeyHash = pubKeyHash;
        return _this;
    }
    ImplicitAccountCreationAddress.prototype.address = function () {
        return new Ed25519Address(this.pubKeyHash);
    };
    ImplicitAccountCreationAddress.prototype.toString = function () {
        return this.address.toString();
    };
    return ImplicitAccountCreationAddress;
}(Address));
var RestrictedAddressDiscriminator = {
    property: 'type',
    subTypes: [
        { value: Ed25519Address, name: AddressType.Ed25519 },
        { value: AccountAddress, name: AddressType.Account },
        { value: NftAddress, name: AddressType.Nft },
    ]
};
/**
 * An address with restricted capabilities.
 */
var RestrictedAddress = /** @class */ (function (_super) {
    __extends(RestrictedAddress, _super);
    /**
     * @param address An address.
     */
    function RestrictedAddress(address) {
        var _this = _super.call(this, AddressType.Restricted) || this;
        _this.address = address;
        return _this;
    }
    RestrictedAddress.prototype.setAllowedCapabilities = function (allowedCapabilities) {
        if (allowedCapabilities.some(function (c) { return c != 0; })) {
            this.allowedCapabilities =
                '0x' +
                    Buffer.from(allowedCapabilities.buffer, allowedCapabilities.byteOffset, allowedCapabilities.byteLength).toString('hex');
        }
        else {
            this.allowedCapabilities = undefined;
        }
    };
    RestrictedAddress.prototype.withAllowedCapabilities = function (allowedCapabilities) {
        this.setAllowedCapabilities(allowedCapabilities);
        return this;
    };
    RestrictedAddress.prototype.getAllowedCapabilities = function () {
        return this.allowedCapabilities !== undefined
            ? Uint8Array.from(Buffer.from(this.allowedCapabilities.substring(2), 'hex'))
            : new Uint8Array();
    };
    RestrictedAddress.prototype.toString = function () {
        return (this.address.toString() +
            (this.allowedCapabilities !== undefined
                ? this.allowedCapabilities.substring(2)
                : ''));
    };
    __decorate([
        Type(function () { return Address; }, {
            discriminator: RestrictedAddressDiscriminator
        })
    ], RestrictedAddress.prototype, "address");
    return RestrictedAddress;
}(Address));
/**
 * A weighted address.
 */
var WeightedAddress = /** @class */ (function () {
    /**
     * @param address The unlocked address.
     * @param weight The weight of the unlocked address.
     */
    function WeightedAddress(address, weight) {
        this.address = address;
        this.weight = weight;
    }
    __decorate([
        Type(function () { return Address; }, {
            discriminator: {
                property: 'type',
                subTypes: [
                    { value: Ed25519Address, name: AddressType.Ed25519 },
                    { value: AccountAddress, name: AddressType.Account },
                    { value: NftAddress, name: AddressType.Nft },
                    { value: AnchorAddress, name: AddressType.Anchor },
                ]
            }
        })
    ], WeightedAddress.prototype, "address");
    return WeightedAddress;
}());
/**
 * An address that consists of addresses with weights and a threshold value.
 * The Multi Address can be unlocked if the cumulative weight of all unlocked addresses is equal to or exceeds the
 * threshold.
 */
var MultiAddress = /** @class */ (function (_super) {
    __extends(MultiAddress, _super);
    /**
     * @param addresses The weighted unlocked addresses.
     * @param threshold The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address.
     */
    function MultiAddress(addresses, threshold) {
        var _this = _super.call(this, AddressType.Multi) || this;
        _this.addresses = addresses;
        _this.threshold = threshold;
        return _this;
    }
    MultiAddress.prototype.toString = function () {
        return JSON.stringify(this);
    };
    return MultiAddress;
}(Address));
var AddressDiscriminator = {
    property: 'type',
    subTypes: [
        { value: Ed25519Address, name: AddressType.Ed25519 },
        { value: AccountAddress, name: AddressType.Account },
        { value: NftAddress, name: AddressType.Nft },
        { value: AnchorAddress, name: AddressType.Anchor },
        {
            value: ImplicitAccountCreationAddress,
            name: AddressType.ImplicitAccountCreation
        },
        { value: MultiAddress, name: AddressType.Multi },
        { value: RestrictedAddress, name: AddressType.Restricted },
    ]
};
export { AddressDiscriminator, Address, AddressType, Ed25519Address, AccountAddress, NftAddress, AnchorAddress, ImplicitAccountCreationAddress, WeightedAddress, MultiAddress, RestrictedAddress, };
//# sourceMappingURL=address.js.map