/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const Utils = require("./../../../unisharp/Utils");
const FileInfo = require("./../../../unisharp/FileInfo");

const ZipEntry = require("./ZipEntry");
const IEntryFactory = require("./IEntryFactory");
const ZipEntryFactoryTimeSetting = require("./ZipEntryFactoryTimeSetting");
const INameTransform = require("./INameTransform");
const ZipNameTransform = require("./ZipNameTransform");

// Basic implementation of <see cref="IEntryFactory"></see>
class ZipEntryFactory extends IEntryFactory {
    
    constructor(timeSetting = ZipEntryFactoryTimeSetting.LASTWRITETIME) {
        super();
        this.nameTransform_ = null;
        this.fixedDateTime_ = Utils.now();
        this.timeSetting_ = ZipEntryFactoryTimeSetting.LASTWRITETIME;
        this.isUnicodeText_ = true;
        this.getAttributes_ = -1;
        this.setAttributes_ = 0;
        this.timeSetting_ = timeSetting;
        this.nameTransform_ = new ZipNameTransform();
    }
    
    get nameTransform() {
        return this.nameTransform_;
    }
    set nameTransform(value) {
        if (value === null) 
            this.nameTransform_ = new ZipNameTransform();
        else 
            this.nameTransform_ = value;
        return value;
    }
    
    get setting() {
        return this.timeSetting_;
    }
    set setting(value) {
        this.timeSetting_ = value;
        return value;
    }
    
    get fixedDateTime() {
        return this.fixedDateTime_;
    }
    set fixedDateTime(value) {
        if (value.getFullYear() < 1970) 
            throw new Error("Value is too old to be valid", "value");
        this.fixedDateTime_ = value;
        return value;
    }
    
    get getAttributes() {
        return this.getAttributes_;
    }
    set getAttributes(value) {
        this.getAttributes_ = value;
        return value;
    }
    
    get setAttributes() {
        return this.setAttributes_;
    }
    set setAttributes(value) {
        this.setAttributes_ = value;
        return value;
    }
    
    get isUnicodeText() {
        return this.isUnicodeText_;
    }
    set isUnicodeText(value) {
        this.isUnicodeText_ = value;
        return value;
    }
    
    makeFileEntry(fileName) {
        return this.makeFileEntryEx(fileName, true);
    }
    
    makeFileEntryEx(fileName, useFileSystem) {
        let result = new ZipEntry(this.nameTransform_.transformFile(fileName));
        result.isUnicodeText = this.isUnicodeText_;
        let externalAttributes = 0;
        let useAttributes = this.setAttributes_ !== 0;
        let fi = null;
        if (useFileSystem) 
            fi = new FileInfo(fileName);
        if ((fi !== null) && fi.exists()) {
            result.size = fi.length;
            useAttributes = true;
        }
        else if (this.timeSetting_ === ZipEntryFactoryTimeSetting.FIXED) 
            result.dateTime = this.fixedDateTime_;
        if (useAttributes) {
            externalAttributes |= this.setAttributes_;
            result.externalFileAttributes = externalAttributes;
        }
        return result;
    }
    
    makeDirectoryEntry(directoryName) {
        return this.makeDirectoryEntryEx(directoryName, true);
    }
    
    makeDirectoryEntryEx(directoryName, useFileSystem) {
        let result = new ZipEntry(this.nameTransform_.transformDirectory(directoryName));
        result.isUnicodeText = this.isUnicodeText_;
        result.size = 0;
        let externalAttributes = 0;
        let di = null;
        if (useFileSystem) 
            di = new DirectoryInfo(directoryName);
        externalAttributes |= ((this.setAttributes_ | 16));
        result.externalFileAttributes = externalAttributes;
        return result;
    }
}


module.exports = ZipEntryFactory