Editor/Cabinet/ArchCabinetCatalog.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sunless.Architecture;

// The carcasses a run can be cut from. What ships stands with NOTHING on disk behind it and an authored file of the
// same name tops it - the folder is not the catalog. Writing the built-ins out on first read is what made hiding
// one worth exactly one load: the next read found the file it had just written and put the type straight back.
public sealed class ArchCabinetCatalog : IArchCatalog {
	public static string Directory => ArchDataRoot.For( "cabinets" );

	public ArchShelf Shelf => ArchShelf.Cabinets;

	public string Named( object entry ) => entry is ArchCabinetType type ? type.Name : null;

	public IEnumerable<object> Shipped() => ArchCabinetTypes.Shipped();

	public IEnumerable<object> Authored() {
		var loaded = new List<ArchCabinetType>();

		foreach ( var file in ArchStorage.DocumentFiles( Directory, ".cabinet.json" ) ) {
			if ( ArchStorage.ReadDocument<ArchCabinetType>( $"{Directory}/{file}" ) is { Name.Length: > 0 } type ) {
				loaded.Add( type );
			}
		}

		return loaded.OrderBy( type => type.Name ).ToList();
	}

	public bool Save( object entry ) => entry is ArchCabinetType type && ArchStorage.WriteDocument( PathFor( type.Name ), type );

	static string PathFor( string name ) => $"{Directory}/{name}.cabinet.json";
}