
 (function () {
  'use strict';

  var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  var applyListFormat = function (editor, listName, styleValue) {
    // editor.dom.getParent(editor.selection.getNode(), 'tp-buttons')

   isColumns(editor, styleValue);
    // editor.execCommand("InsertUnorderedList", !1, _value)
     
                   
  };

  // public\tinymce\plugins\tpIconlists\tpIconlists.css
  var isColumns = function(editor, styleValue) {
     let _parent = editor.dom.getParent(editor.selection.getNode(),'ol,ul,dl')
     if(_parent){
     let _calss = editor.dom.getAttrib(_parent, 'class')
     let oldClass = _calss.match(/tp-columns_([A-Za-z0-9-]+)/)
      editor.dom.removeClass('tp-columns')
      oldClass &&  editor.dom.removeClass(_parent, oldClass[0])
      oldClass && oldClass[1] === styleValue ? editor.dom.removeClass(_parent, 'tp-columns ' + oldClass[0]) : editor.dom.addClass(_parent, 'tp-columns tp-columns_'+styleValue)
     }else{
      let _list = editor.selection.getSelectedBlocks();
      if(typeof _list ==='object' && _list.length>1){
        editor.execCommand("InsertUnorderedList", !1, { 'list-style-type': 'default',"list-attributes": { class: 'tp-columns tp-columns_' + styleValue } })
      }
    }
    
  }
  var register = function (editor) {
  
    editor.addCommand('ApplyIconListsStyle', function (ui, value) { 
      applyListFormat(editor, 'UL', value['class']);
    });
  };

  var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');

  var getColumnValue = function (editor) {
    var _Value  = editor.getParam('tp_iconlists_styles', 'columns-2,columns-3');
    return _Value ? _Value.split(/[ ,]/) : [];
  };

  var noop = function () {
  };
  var constant = function (value) {
    return function () {
      return value;
    };
  };
  var never = constant(false);
  var always = constant(true);

  var none = function () {
    return NONE;
  };
  var NONE = function () {
    var eq = function (o) {
      return o.isNone();
    };
    var call = function (thunk) {
      return thunk();
    };
    var id = function (n) {
      return n;
    };
    var me = {
      fold: function (n, _s) {
        return n();
      },
      is: never,
      isSome: never,
      isNone: always,
      getOr: id,
      getOrThunk: call,
      getOrDie: function (msg) {
        throw new Error(msg || 'error: getOrDie called on none.');
      },
      getOrNull: constant(null),
      getOrUndefined: constant(undefined),
      or: id,
      orThunk: call,
      map: none,
      each: noop,
      bind: none,
      exists: never,
      forall: always,
      filter: none,
      equals: eq,
      equals_: eq,
      toArray: function () {
        return [];
      },
      toString: constant('none()')
    };
    return me;
  }();
  var some = function (a) {
    var constant_a = constant(a);
    var self = function () {
      return me;
    };
    var bind = function (f) {
      return f(a);
    };
    var me = {
      fold: function (n, s) {
        return s(a);
      },
      is: function (v) {
        return a === v;
      },
      isSome: always,
      isNone: never,
      getOr: constant_a,
      getOrThunk: constant_a,
      getOrDie: constant_a,
      getOrNull: constant_a,
      getOrUndefined: constant_a,
      or: self,
      orThunk: self,
      map: function (f) {
        return some(f(a));
      },
      each: function (f) {
        f(a);
      },
      bind: bind,
      exists: bind,
      forall: bind,
      filter: function (f) {
        return f(a) ? me : NONE;
      },
      toArray: function () {
        return [a];
      },
      toString: function () {
        return 'some(' + a + ')';
      },
      equals: function (o) {
        return o.is(a);
      },
      equals_: function (o, elementEq) {
        return o.fold(never, function (b) {
          return elementEq(a, b);
        });
      }
    };
    return me;
  };
  var from = function (value) {
    return value === null || value === undefined ? NONE : some(value);
  };
  var Optional = {
    some: some,
    none: none,
    from: from
  };

  var isChildOfBody = function (editor, elm) {
    return editor.$.contains(editor.getBody(), elm);
  };
  var isTableCellNode = function (node) {
    return node && /^(TH|TD)$/.test(node.nodeName);
  };
  var isListNode = function (editor) {
    return function (node) {
      return node && /^(OL|UL|DL)$/.test(node.nodeName) && isChildOfBody(editor, node);
    };
  };
  var getSelectedStyleType = function (editor) {
    var listElm = editor.dom.getParent(editor.selection.getNode(), 'ol,ul');
    var style = editor.dom.getStyle(listElm, 'listStyleType');
    return Optional.from(style);
  };

  var findIndex = function (list, predicate) {
    for (var index = 0; index < list.length; index++) {
      var element = list[index];
      if (predicate(element)) {
        return index;
      }
    }
    return -1;
  };
  var styleValueToText = function (styleValue) {
    return styleValue.replace(/\-/g, ' ').replace(/\b\w/g, function (chr) {
      return chr.toUpperCase();
    });
  };
  var isWithinList = function (editor, e, nodeName) {
    var tableCellIndex = findIndex(e.parents, isTableCellNode);
    var parents = tableCellIndex !== -1 ? e.parents.slice(0, tableCellIndex) : e.parents;
    var lists = global$1.grep(parents, isListNode(editor));
    return lists.length > 0 && lists[0].nodeName === nodeName;
  };
  var addSplitButton = function (editor, id, tooltip, cmd, nodeName, styles) {
    editor.ui.registry.addSplitButton(id, {
      tooltip: tooltip,
      icon: id,
      presets: 'listpreview',
      columns: 3,
      fetch: function (callback) {
        var items = global$1.map(styles, function (styleValue) {
          var iconStyle = nodeName === 'OL' ? 'num' : 'bull';
          var iconName = styleValue === 'disc' || styleValue === 'decimal' ? 'default' : styleValue;
          var itemValue = styleValue === 'default' ? 'columns-2' : styleValue;
          var displayText = styleValueToText(styleValue);
          return {
            type: 'choiceitem',
            value: itemValue,
            icon: 'tp-columns-' + iconName,
            text: displayText
          };
        });
        callback(items);
      },
      onAction: function () {
        // return editor.execCommand(cmd);
      },
      onItemAction: function (_splitButtonApi, value) {
        applyListFormat(editor, nodeName, value);
      },
      select: function (value) {
        var listStyleType = getSelectedStyleType(editor);
        return listStyleType.map(function (listStyle) {
          return value === listStyle;
        }).getOr(false);
      },
      onSetup: function (api) {
        // var nodeChangeHandler = function (e) {
        //   api.setActive(isWithinList(editor, e, nodeName));
        // };
        // editor.on('NodeChange', nodeChangeHandler);
        // return function () {
        //   return editor.off('NodeChange', nodeChangeHandler);
        // };
      }
    });
  };
  var addButton = function (editor, id, tooltip, cmd, nodeName, _styles) {
    editor.ui.registry.addToggleButton(id, {
      active: false,
      tooltip: tooltip,
      icon: nodeName === 'OL' ? 'ordered-list' : 'unordered-list',
      onSetup: function (api) {
        var nodeChangeHandler = function (e) {
          api.setActive(isWithinList(editor, e, nodeName));
        };
        editor.on('NodeChange', nodeChangeHandler);
        return function () {
          return editor.off('NodeChange', nodeChangeHandler);
        };
      },
      onAction: function () {
        return editor.execCommand(cmd);
      }
    });
  };
  var addControl = function (editor, id, tooltip, cmd, nodeName, styles) {
    if (styles.length > 1) {
      addSplitButton(editor, id, tooltip, cmd, nodeName, styles);
    } else {
      addButton(editor, id, tooltip, cmd, nodeName);
    }
  };
  var register$1 = function (editor) {
    // addControl(editor, 'numlist', 'Numbered list', 'InsertOrderedList', 'OL', getNumberStyles(editor));
    addControl(editor, 'tpColumns', 'Horizontal columns', 'InsertUnorderedList', 'UL', getColumnValue(editor));
  };

  function Plugin () {
    global.add('tpColumns', function (editor) {
        register$1(editor);
        // register(editor);
    })
     
  }

  Plugin();
}());
