import { filterTruthy } from "./filters.ts";

export const deserializeMCProperties = (serialized: string) => {
  const propertiesMap = new Map<string,string>();

  const commentRegex = /#.+\r?\n/g;
  serialized.replace(commentRegex, '')
    .split(/\r?\n/)
    .filter(filterTruthy)
    .forEach(prop => {
      const [key, value] = prop.split('=');
      propertiesMap.set(key, value ?? '');
    });

  return propertiesMap;
}

export const serializeMCProperties = (deserialized: Map<string,string>) => {
  let text = '';
  for (const [key,value] of deserialized.entries()) {
    text += `${key}=${value}\n`;
  }
  return text;
}