package jxl.write.biff;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public class SharedStrings {
    private HashMap strings = new HashMap(100);
    private ArrayList stringList = new ArrayList(100);
    private int totalOccurrences = 0;

    private SSTContinueRecord createContinueRecord(String str, int i2, File file) throws IOException {
        SSTContinueRecord sSTContinueRecord = null;
        while (i2 != 0) {
            sSTContinueRecord = new SSTContinueRecord();
            i2 = (i2 == str.length() || str.length() == 0) ? sSTContinueRecord.setFirstString(str, true) : sSTContinueRecord.setFirstString(str.substring(str.length() - i2), false);
            if (i2 != 0) {
                file.write(sSTContinueRecord);
                sSTContinueRecord = new SSTContinueRecord();
            }
        }
        return sSTContinueRecord;
    }

    public String get(int i2) {
        return (String) this.stringList.get(i2);
    }

    public int getIndex(String str) {
        Integer num = (Integer) this.strings.get(str);
        if (num == null) {
            num = new Integer(this.strings.size());
            this.strings.put(str, num);
            this.stringList.add(str);
        }
        this.totalOccurrences++;
        return num.intValue();
    }

    public void write(File file) throws IOException {
        SSTRecord sSTRecord = new SSTRecord(this.totalOccurrences, this.stringList.size());
        ExtendedSSTRecord extendedSSTRecord = new ExtendedSSTRecord(this.stringList.size());
        int numberOfStringsPerBucket = extendedSSTRecord.getNumberOfStringsPerBucket();
        Iterator it = this.stringList.iterator();
        int i2 = 0;
        String str = null;
        int i3 = 0;
        while (it.hasNext() && i2 == 0) {
            str = (String) it.next();
            int offset = sSTRecord.getOffset() + 4;
            int iAdd = sSTRecord.add(str);
            if (i3 % numberOfStringsPerBucket == 0) {
                extendedSSTRecord.addString(file.getPos(), offset);
            }
            i3++;
            i2 = iAdd;
        }
        file.write(sSTRecord);
        if (i2 != 0 || it.hasNext()) {
            SSTContinueRecord sSTContinueRecordCreateContinueRecord = createContinueRecord(str, i2, file);
            while (it.hasNext()) {
                String str2 = (String) it.next();
                int offset2 = sSTContinueRecordCreateContinueRecord.getOffset() + 4;
                int iAdd2 = sSTContinueRecordCreateContinueRecord.add(str2);
                if (i3 % numberOfStringsPerBucket == 0) {
                    extendedSSTRecord.addString(file.getPos(), offset2);
                }
                i3++;
                if (iAdd2 != 0) {
                    file.write(sSTContinueRecordCreateContinueRecord);
                    sSTContinueRecordCreateContinueRecord = createContinueRecord(str2, iAdd2, file);
                }
            }
            file.write(sSTContinueRecordCreateContinueRecord);
        }
        file.write(extendedSSTRecord);
    }
}
