// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fs

import (
	
	
	
)

// ReadDirFS is the interface implemented by a file system
// that provides an optimized implementation of [ReadDir].
type ReadDirFS interface {
	FS

	// ReadDir reads the named directory
	// and returns a list of directory entries sorted by filename.
	ReadDir(name string) ([]DirEntry, error)
}

// ReadDir reads the named directory
// and returns a list of directory entries sorted by filename.
//
// If fs implements [ReadDirFS], ReadDir calls fs.ReadDir.
// Otherwise ReadDir calls fs.Open and uses ReadDir and Close
// on the returned file.
func ( FS,  string) ([]DirEntry, error) {
	if ,  := .(ReadDirFS);  {
		return .ReadDir()
	}

	,  := .Open()
	if  != nil {
		return nil, 
	}
	defer .Close()

	,  := .(ReadDirFile)
	if ! {
		return nil, &PathError{Op: "readdir", Path: , Err: errors.New("not implemented")}
	}

	,  := .ReadDir(-1)
	slices.SortFunc(, func(,  DirEntry) int {
		return bytealg.CompareString(.Name(), .Name())
	})
	return , 
}

// dirInfo is a DirEntry based on a FileInfo.
type dirInfo struct {
	fileInfo FileInfo
}

func ( dirInfo) () bool {
	return .fileInfo.IsDir()
}

func ( dirInfo) () FileMode {
	return .fileInfo.Mode().Type()
}

func ( dirInfo) () (FileInfo, error) {
	return .fileInfo, nil
}

func ( dirInfo) () string {
	return .fileInfo.Name()
}

func ( dirInfo) () string {
	return FormatDirEntry()
}

// FileInfoToDirEntry returns a [DirEntry] that returns information from info.
// If info is nil, FileInfoToDirEntry returns nil.
func ( FileInfo) DirEntry {
	if  == nil {
		return nil
	}
	return dirInfo{fileInfo: }
}