const LinksColumn = {
    name: 'LinksColumn',
    components: {
        LinksyButton,
        LinksyDropdown
    },
    props: {
        item: Object,
        active: Boolean,
       	isSelectableSource: { type: Boolean, default: true},
        isAnchorReplace: { type: Boolean, default: false},
        isItemSelected: Function
    },
    directives: {
        'linksy-dom-text': LinksyDOMText,
        'linksy-tooltip': LinksyTooltip,
    },
    data() {
        return {
            opened: false,
        }
    },
    computed: {
        allLinksSelectedInColumn() {
            return this.item.sources.every(source => this.isItemSelected(this.item, source, true));
        },
        isActiveItem: {
            get() {
                return this.item.sources.length === 1
            }
        }
    },
    methods: {
        handleSelectAll() {
            this.$emit("onSelectAll", this.item);
        },
		onSelect(source, isTableBased) {
			this.$emit("onSelect", this.item, source, isTableBased);
		},
        closeActiveItem(index) {
            if (index > 0) {
                return
            }

			this.$emit('on-close-item')
		},
        totalResults() {
            return this.item.sources.length;
        },
        isArray(value) {
			return Array.isArray(value);
		},
        toggleActive() {
            this.$emit('on-open-item')
		},

    },
    template: `
    <div class="columns" style="max-height: 350px; overflow-y: auto; overflow-x: hidden;">
        <div v-if="!isActiveItem">
            <div @click="toggleActive" v-if="!active" class="position-relative column collasped text-center p-2 m-0 fs-6 cursor-pointer">
                <span>Sources: <span class="item-count">{{totalResults()}}</span></span>

                <span class="toggle-caret position-absolute"><i class="fa" :class="'fa-angle-' + (active ? 'up' : 'down')"></i></span>
            </div>
        </div>
        <div v-if="active" class="column expanded">
            <div v-for="(source, index) in item.sources">
                <div class="link-anchor-container d-flex flex-column gap-2">
                    <div class="border-bottom-0 d-flex justify-content-between align-items-center position-relative cursor-pointer">
                        <div class="item-title position-relative d-flex align-items-center gap-2">
                            <div class="d-flex align-items-center gap-2"  @click="onSelect(source, false)">
                                <span v-if="isSelectableSource" class="linksy-checkbox" :class="{'checked': isItemSelected(item, source, false), 'is-disabled': isProcessing}" />
                                <h4 class="m-0 fw-medium">{{source.source}}</h4>
                            </div>
                            <div class="item-direct-links position-absolute">
                                <a :href="source.view_link" target="_blank">View</a>
                                <a :href="source.edit_link" target="_blank">Edit</a>
                            </div>
                        </div>
                        <span v-if="index === 0 && !isActiveItem" @click="closeActiveItem(index)"><i class="fa" :class="'fa-angle-' + (active ? 'up' : 'down')"></i></span>
                    </div>
                    <div v-if="!isAnchorReplace" class="db-column-content">
                        <div v-for="(anchor, anchorIndex) in source.anchors">
                            <div class="d-flex flex-column gap-2">
                                <h5 class="m-0 d-flex align-items-start gap-2">
                                    <i class="fa fa-anchor mt-1"></i>
                                    <span class="fw-medium fs-16px" v-linksy-dom-text>"{{anchor.anchor}}"</span>
                                </h5>
                            </div>
                        </div>
                    </div>
                    <div v-else class="db-column-content">
                        <div v-for="(link, linkIndex) in source.links">
                            <div class="d-flex flex-column gap-2">
                                <h5 class="m-0 d-flex align-items-start gap-2">
                                    <i class="fa fa-link mt-1"></i>
                                    <span class="fw-medium fs-16px text-primary">"{{link.link}}"</span>
                                </h5>
                            </div>
                        </div>
                    </div>
                </div>
                <hr class="m-0" v-if="index != item.sources.length - 1" />
            </div>

            <div class="sticky-buttons-container position-sticky bottom-0 z-1">
                <hr v-if="!isActiveItem" class="m-0" />

				<div v-if="!isActiveItem" class="action-buttons">
					<linksy-button @click="handleSelectAll" variant="white" class="d-flex align-items-center gap-2">
						<span class="linksy-checkbox" :class="{'checked': allLinksSelectedInColumn}"></span>
						Select all
					</linksy-button>
				</div>
            </div>
        </div>
    </div>
    `,
}