"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.saveStyle = saveStyle;
exports.updateStyle = updateStyle;
exports.isCustomStyleExists = isCustomStyleExists;
exports.getCustomStyles = getCustomStyles;
exports.getCustomStyleByName = getCustomStyleByName;
exports.getCustomStyleByLevel = getCustomStyleByLevel;
exports.removeStyle = removeStyle;
exports.renameStyle = renameStyle;

var _http = require("./client/http");

// [FS] IRAD-1085 2020-10-09
// Handle custom style in local storage
function buildRoute() {
  const root = '/style-service';

  for (var _len = arguments.length, path = new Array(_len), _key = 0; _key < _len; _key++) {
    path[_key] = arguments[_key];
  }

  return [root, ...path].join('/');
}

let customStyles = []; // [FS] IRAD-1128 2020-12-29
// save the custom style to server

function saveStyle(style) {
  customStyles = [];
  let styles;
  const url = buildRoute('savecustomstyle');
  (0, _http.POST)(url, JSON.stringify(style), 'application/json; charset=utf-8').then(data => {
    styles = JSON.parse(data);
    customStyles = styles;
  }, err => {});
} // [FS] IRAD-1133 2021-01-05
// update and save the custom style to server


function updateStyle(style) {
  let styles;
  return new Promise((resolve, reject) => {
    const url = buildRoute('savecustomstyle');
    (0, _http.POST)(url, JSON.stringify(style), 'application/json; charset=utf-8').then(data => {
      styles = JSON.parse(data);
      resolve(styles);
      customStyles = styles;
    }, err => {
      styles = null;
      resolve(styles);
    });
  });
} // [FS] IRAD-1137 2021-01-15
// check if the entered style name already exist


function isCustomStyleExists(styleName) {
  let bOK = false;

  if (customStyles.length > 0) {
    for (const style of customStyles) {
      if (styleName === style.styleName) {
        bOK = true;
        return bOK;
      }
    }
  }

  return bOK;
}

function getStyles() {
  let style;

  if (customStyles.length > 0) {
    return new Promise((resolve, reject) => {
      resolve(customStyles);
    });
  }

  return new Promise((resolve, reject) => {
    const url = buildRoute('getcustomstyles');
    (0, _http.GET)(url).then(data => {
      style = JSON.parse(data);
      resolve(style);
      customStyles = style;
    }, err => {
      style = null;
      resolve(style);
    });
  });
}

async function getCustomStyles() {
  return await getStyles();
} // [FS] IRAD-1128 2020-12-30
// get a style by styleName


function getCustomStyleByName(name) {
  let style = null;

  if (customStyles.length > 0) {
    for (const obj of customStyles) {
      if (name === obj.styleName) {
        style = obj.styles;
      }
    }
  } else {
    const customStyles = getCustomStyles();
    customStyles.then(result => {
      if (null != result) {
        result.forEach(obj => {
          if (name === obj.styleName) {
            style = obj.styles;
          }
        });
        return style;
      }

      return style;
    });
  }

  return style;
} // get a style by Level


function getCustomStyleByLevel(level) {
  let style = null;

  if (customStyles.length > 0) {
    for (const obj of customStyles) {
      if (obj.styles.styleLevel && level === Number(obj.styles.styleLevel)) {
        if (null === style) {
          style = obj;
        }
      }
    }
  } else {
    const customStyles = getCustomStyles();
    customStyles.then(result => {
      if (null != result) {
        for (const obj of customStyles) {
          if (obj.styles.styleLevel && level === Number(obj.styles.styleLevel)) {
            if (null === style) {
              style = obj;
            }
          }
        }
      }

      return style;
    });
  }

  return style;
} // [FS] IRAD-1128 2020-12-29
// to remove the selected Custom style.


function removeStyle(name) {
  customStyles = [];
  const url = buildRoute('removecustomstyle');
  (0, _http.POST)(url, name, 'text/plain').then(data => {
    customStyles = data;
    console.log(data);
  }, err => {});
} // [FS] IRAD-1128 2020-12-29
// save the custom style to server


function renameStyle(oldStyleName, newStyleName) {
  customStyles = [];
  let styles = null;
  const obj = {
    styleName: oldStyleName,
    modifiedStyleName: newStyleName
  };
  return new Promise((resolve, reject) => {
    const url = buildRoute('renamecustomstyle');
    (0, _http.POST)(url, JSON.stringify(obj), 'application/json; charset=utf-8').then(data => {
      styles = JSON.parse(data);
      resolve(styles);
      customStyles = styles;
    }, err => {
      styles = null;
      resolve(styles);
    });
  });
}