# frozen_string_literal: true

# This file is part of RASN1
# See https://codeberg.org/lemontree55/rasn1 for more informations
# Copyright (C) 2016-2024 Sylvain Daubert <sylvain.daubert@laposte.net>
# Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
# This program is published under MIT license.

module RASN1
  module Types
    # @abstract This class SHOULD be used as base class for all ASN.1 primitive
    #  types.
    # This is the base class for all ASN.1 constructed types. A constructed type combines similar or different
    # {Primitive primitive} types into ordered or unordered groups
    #
    # @author Sylvain Daubert
    class Constructed < Base
      # Constructed value
      ASN1_PC = 0x20

      # @return [Boolean]
      # @since 0.12.0
      # @see Base#can_build?
      def can_build? # rubocop:disable Metrics/CyclomaticComplexity
        return super unless @value.is_a?(Array) && optional?
        return false unless super

        @value.any? do |el|
          el.can_build? && (
            el.primitive? ||
              (el.value.respond_to?(:empty?) ? !el.value.empty? : !el.value.nil?))
        end
      end

      # @param level [::Integer] (default: 0)
      # @return [String]
      def inspect(level=0)
        case @value
        when Array
          str = common_inspect(level)
          str << "\n"
          level = level.abs + 1
          @value.each do |item|
            case item
            when Base, Model, Wrapper
              str << "#{item.inspect(level)}\n"
            else
              str << '  ' * level
              str << "#{item.inspect}\n"
            end
          end
          str
        else
          super
        end
      end
    end
  end
end
