{% extends "layout.njk" %}
{% import "macros.njk" as macros %}

{% block main %}

{{ imports }}

{{ macros.docblock(account.docs) }}
{{ typeManifest.type }}

{% for nestedStruct in typeManifest.nestedStructs %}
{{ nestedStruct }}
{% endfor %}

impl {{ account.name | pascalCase }} {
  {% if account.size != null %}
    pub const LEN: usize = {{ account.size }};
  {% endif %}

  {% if constantSeeds.length > 0 %}
    {% set index = 0 %}
    /// Prefix values used to generate a PDA for this account.
    ///
    /// Values are positional and appear in the following order:
    ///
    {% for seed in seeds %}
      {% if seed.kind === 'constantPdaSeedNode' and seed.value.kind === 'programIdValueNode' %}
        ///   {{ loop.index0 }}. `crate::{{ program.name | snakeCase | upper }}_ID`
      {% elif seed.kind === 'constantPdaSeedNode' %}
        ///   {{ loop.index0 }}. `{{ account.name | pascalCase }}::PREFIX{{ '.' + index if constantSeeds.length > 1 }}`
        {% set index = index + 1 %}
      {% else %}
        ///   {{ loop.index0 }}. {{ seed.name | snakeCase }} (`{{ seed.typeManifest.type }}`)
      {% endif %}
    {% endfor %}
    {% if constantSeeds.length > 1 %}
      pub const PREFIX: (
      {% for seed in constantSeeds %}
        &'static [u8],
      {% endfor %}
      ) = (
      {% for seed in constantSeeds %}
        {{ seed.value.render }}.as_bytes(),
      {% endfor %}
      );
    {% elif constantSeeds.length === 1 %}
      pub const PREFIX: &'static [u8] = {{ constantSeeds[0].value.render }}.as_bytes();
    {% endif %}
  {% endif %}

  {% if pda %}
    pub fn create_pda(
      {% if hasVariableSeeds %}
          {% for seed in seeds %}
            {% if seed.kind === 'variablePdaSeedNode' %}
              {{ seed.name | snakeCase }}: {{ seed.typeManifest.type }},
            {% endif %}
          {% endfor %}
      {% endif %}
      bump: u8,
    ) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
      solana_program::pubkey::Pubkey::create_program_address(
        &[
          {% for seed in seeds %}
            {% if seed.kind === 'constantPdaSeedNode' and seed.value.kind === 'programIdValueNode' %}
              crate::{{ program.name | snakeCase | upper }}_ID.as_ref(),
            {% elif seed.kind === 'constantPdaSeedNode' %}
              {{ seed.value.render }}.as_bytes(),
            {% elif seed.kind == 'variablePdaSeedNode' and seed.resolvedType.kind == 'publicKeyTypeNode' %}
              {{ seed.name | snakeCase }}.as_ref(),
            {% elif seed.kind == 'variablePdaSeedNode' and seed.resolvedType.kind == 'bytesTypeNode' %}
              &{{ seed.name | snakeCase }},
            {% else %}
              {{ seed.name | snakeCase }}.to_string().as_ref(),
            {% endif %}
          {% endfor %}
          &[bump],
        ],
        &crate::{{ program.name | snakeCase | upper }}_ID,
      )
    }

    pub fn find_pda(
    {% if hasVariableSeeds %}
        {% for seed in seeds %}
          {% if seed.kind == 'variablePdaSeedNode' and seed.resolvedType.kind == 'publicKeyTypeNode' %}
            {{ seed.name | snakeCase }}: &{{ seed.typeManifest.type }},
          {% elif seed.kind === 'variablePdaSeedNode' %}
            {{ seed.name | snakeCase }}: {{ seed.typeManifest.type }},
          {% endif %}
        {% endfor %}
    {% endif %}
    ) -> (solana_program::pubkey::Pubkey, u8) {
      solana_program::pubkey::Pubkey::find_program_address(
        &[
          {% for seed in seeds %}
            {% if seed.kind === 'constantPdaSeedNode' and seed.value.kind === 'programIdValueNode' %}
              crate::{{ program.name | snakeCase | upper }}_ID.as_ref(),
            {% elif seed.kind === 'constantPdaSeedNode' %}
              {{ seed.value.render }}.as_bytes(),
            {% elif seed.kind == 'variablePdaSeedNode' and seed.resolvedType.kind == 'publicKeyTypeNode' %}
              {{ seed.name | snakeCase }}.as_ref(),
            {% elif seed.kind == 'variablePdaSeedNode' and seed.resolvedType.kind == 'bytesTypeNode' %}
              &{{ seed.name | snakeCase }},
            {% else %}
              {{ seed.name | snakeCase }}.to_string().as_ref(),
            {% endif %}
          {% endfor %}
        ],
        &crate::{{ program.name | snakeCase | upper }}_ID,
      )
    }
  {% endif %}

  #[inline(always)]
  pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
    let mut data = data;
    Self::deserialize(&mut data)
  }
}

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for {{ account.name | pascalCase }} {
  type Error = std::io::Error;

  fn try_from(account_info: &solana_program::account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
      let mut data: &[u8] = &(*account_info.data).borrow();
      Self::deserialize(&mut data)
  }
}

{% endblock %}
