content: /*
   * DazzleConf
   * Copyright © 2025 Anand Beh
   *
   * DazzleConf is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * DazzleConf is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with DazzleConf. If not, see <https://www.gnu.org/licenses/>
   * and navigate to version 3 of the GNU Lesser General Public License.
   */
  
  package space.arim.dazzleconf;
  
  import space.arim.dazzleconf.backend.CommentData;
  import space.arim.dazzleconf.backend.DataEntry;
  import space.arim.dazzleconf.engine.CommentLocation;
  
  final class ModifyComments {
  
      private static final CommentLocation[] commentLocations = CommentLocation.values();
  
      private final boolean[] writeEntryComments;
      private final boolean writeAllEntryComments; // Fast-path
  
      ModifyComments(ConfigurationDefinition.WriteOptions initFrom) {
          boolean[] writeEntryComments = new boolean[commentLocations.length];
          for (int n = 0; n < writeEntryComments.length; n++) {
              writeEntryComments[n] = initFrom.writeEntryComments(commentLocations[n]);
          }
          boolean writeAllEntryComments = writeEntryComments[0] && writeEntryComments[1] && writeEntryComments[2];
          this.writeEntryComments = writeEntryComments;
          this.writeAllEntryComments = writeAllEntryComments;
      }
  
      boolean writeEntryComments(CommentLocation location) {
          return writeAllEntryComments || writeEntryComments[location.ordinal()];
      }
  
      boolean isAnyLocationEnabled() {
          return writeAllEntryComments || writeEntryComments[0] || writeEntryComments[1] || writeEntryComments[2];
      }
  
      CommentData applyTo(CommentData original, CommentData sourceFrom) {
          if (writeAllEntryComments) {
              return sourceFrom;
          }
          for (CommentLocation location : commentLocations) {
              if (writeEntryComments[location.ordinal()]) {
                  original = original.setAt(location, sourceFrom.getAt(location));
              }
          }
          return original;
      }
  
      DataEntry applyTo(DataEntry subject, CommentData sourceFrom) {
          return subject.withComments(applyTo(subject.getComments(), sourceFrom));
      }
  }
  