// Copyright 2024 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package compression

import (
	"fmt"
	"io"

	"github.com/klauspost/compress/zstd"
)

type writer struct {
	w          io.WriteCloser
	zstdWriter io.WriteCloser
}

// NewWriter creates a new instance of a compression writer with a given compression level.
// Every Write to it is compressed and passed to an inner writer.
// On Close, both writers are closed.
func NewWriter(w io.WriteCloser, level int) (io.WriteCloser, error) {
	zstWriter, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
	if err != nil {
		return nil, fmt.Errorf("failed to create zstd writer: %w", err)
	}

	return &writer{
		w:          w,
		zstdWriter: zstWriter,
	}, nil
}

func (cw *writer) Write(data []byte) (int, error) {
	return cw.zstdWriter.Write(data)
}

func (cw *writer) Close() error {
	err := cw.zstdWriter.Close()
	if err != nil {
		return fmt.Errorf("failed to close zstd writer: %w", err)
	}

	return cw.w.Close()
}
