RSpec.describe FlatKeys do
  describe '#as_nested_structure' do
    it "works" do
      result = FlatKeys.as_nested_structure([
        'root_one',
        'inventories.location.site',
        'ppls.distribution_cables',
        'dslams.port_spec',
        'root_two',
        'dslams.other_spec',
        'root_three',
        #'dslams.other_spec.and_then.some', # should raise
        'very.deep.nested.structure.just.because.we.can',
        'very.deep.nested.structure.with.other.tail',
        'start.with.more.than.what.comes.after',
        'start.with.more.than.what.comes',
        'start.with.less.than.what.comes',
        'start.with.less.than.what.comes2.after',
        'start.with.less.than.what.comes.after',
        'start.with.less.than.what.comes2.after', # duplicate, should be no problem
      ])

      expect( result ).to eq [
        :root_one,
        :root_two,
        :root_three,
        {
          inventories: {location: :site},
          ppls: :distribution_cables,
          dslams: [:port_spec, :other_spec],
          very: {
            deep: {
              nested: {
                structure: {
                  just: {
                    because: {we: :can}
                  },
                  with: {other: :tail}
                }
              }
            }
          },
          start: {
            with: {
              more: {
                than: {what: {comes: :after}}
              },
              less: {
                than: {what: {comes: :after, comes2: :after}}
              }
            }
          }
        }
      ]
    end

    it "removes root duplicates" do
      result = FlatKeys.as_nested_structure(%w[
        sections
        sections.tables
        sections.section_areas
        sections.section_elements
        product_categories
        product_categories.products
        product_categories.products.product_variants
      ])
      expect( result ).to eq [{
        product_categories: {products: :product_variants},
        sections: [:tables, :section_areas, :section_elements]
      }]
    end
  end
end
